利用for循环,计算输出1+2+...+100的和。
#include <iostream> using namespace std; int main(){ int sum = 0; for(int i=1;i<=100;i++) sum = sum + i; cout << sum << endl; return 0; }
输出1~100之间所有偶数。
#include <iostream> using namespace std; int main(){ for(int i=1;i<=100;i++) if(i%2==0) cout << i << " "; cout << endl; return 0; }
利用for循环,分别计算1~100中的奇数的和、偶数的和。
#include <iostream> using namespace std; int main(){ int odd=0,even=0; for(int i=1;i<=100;i++) if(i%2==0){ even += i; }else{ odd += i; } cout << "odd sum is " << odd << ", even sum is " << even << endl; return 0; }
#include <iostream> using namespace std; int main(){ int odd=0,even=0; for(int i=2,j=1;i<=100;i+=2,j+=2){ even += i; odd +=j; } cout << "odd sum is " << odd << ", even sum is " << even << endl; return 0; }
利用for循环计算n!的值。
#include <iostream> using namespace std; int main(){ int n; long long p = 1; cin >> n; for(int i=1;i<=n;i++) p = p * i; printf("%lld\n",p); return 0; }
#include <iostream> using namespace std; int main(){ int n,a; double sum = 0; cin >> n; for (int i=0;i<n;i++){ cin >> a; sum = sum + a; } printf("%.2f",sum/n); }
#include <iostream> using namespace std; int main(){ double a; double sum = 0; for (int i=0;i<12;i++){ cin >> a; sum = sum + a; } printf("$%.2f",sum/12); }
#include <iostream> using namespace std; int main(){ int n; double a; double sum = 0; cin >> n; for (int i=0;i<n;i++){ cin >> a; sum = sum + a; } printf("%.4f",sum/n); }
#include <iostream> using namespace std; int main(){ int n,a,sum = 0; cin >> n; for (int i=0;i<n;i++){ cin >> a; sum = sum + a; } printf("%d %.5f",sum,(double)sum/n); return 0; }
#include <iostream> using namespace std; //05:最高的分数 int main(){ int n,a,max = 0; cin >> n; for (int i=0;i<n;i++){ cin >> a; if (a>max){ max = a; } } printf("%d",max); return 0; }
#include <iostream> using namespace std; //06:整数序列的元素最大跨度值 int main(){ int n,a,max = 0,min=10000; cin >> n; for (int i=0;i<n;i++){ cin >> a; if (a>max){ max = a; } if(a<min){ min = a; } } printf("%d",max-min); return 0; }
#include <iostream> using namespace std; //07:奥运奖牌计数 int main(){ int n,g,s,c,sg=0,ss=0,sc=0; cin >> n; for (int i=0;i<n;i++){ cin >> g >> s >> c; sg = sg + g; ss = ss + s; sc = sc + c; } printf("%d %d %d %d",sg,ss,sc,sg+ss+sc); return 0; }
#include <iostream> using namespace std; //08:多边形内角和 int main(){ int n,a,sum=0; cin >> n; for (int i=1;i<n;i++){ cin >> a; sum = sum + a; } printf("%d",(n-2)*180-sum); return 0; }
#include <iostream> using namespace std; int main(){ int m,n; int sum = 0; cin >> m >> n; for(int i=m;i<=n;i++){ if(i%2==1){ sum = sum + i; } } cout << sum << endl; return 0; }
#include <iostream> using namespace std; //10:满足条件的数累加 int main(){ int m,n,sum=0; cin >> m >> n; for (int i=m;i<=n;i++){ if(i%17==0){ sum = sum + i; } } printf("%d",sum); return 0; }
#include <iostream> using namespace std; //11:整数的个数 int main(){ int n,a,s1=0,s5=0,s10=0; cin >> n; for (int i=0;i<n;i++){ cin >> a; if(a==1){ s1 = s1 + 1; } if(a==5){ s5 = s5 + 1; } if(a==10){ s10 = s10 + 1; } } cout << s1 << endl; cout << s5 << endl; cout << s10 << endl; return 0; }
#include <iostream> using namespace std; int main() { int N,m,a,sum=0; cin >> N >> m; for (int i=0;i<N;i++){ cin >> a; if(a==m){ sum = sum + 1; } } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main() { int a,n,mul=1; cin >> a >> n; for(int i=0;i<n;i++) { mul = mul * a; } cout << mul << endl; return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { double x,n; cin >> x >> n; printf("%.4f",x*pow((1+0.001),n)); return 0; }
#include <iostream> using namespace std; int main() { double R,M,Y; cin >> R >> M >> Y; for (int i=0;i<Y;i++) { M = M * (1 + R/100); } printf("%d",(int)M); return 0; }
#include <iostream> using namespace std; int main(){ double sumn,hp=200,n,k; int m=1; cin >> n >> k; sumn = n; while(sumn<hp && m <40){ sumn = sumn + n; hp = hp * (1+k/100); m = m + 1; } if(m<=20) cout << m << endl; else cout << "Impossible" << endl; return 0; }
#include <iostream> using namespace std; int main() { int k,first=1,second=1,fb; cin >> k; if (k > 2) for (int i=3;i<=k;i++) { fb = first + second; first = second; second = fb; } else fb = 1; cout << fb << endl; return 0; }
#include <iostream> using namespace std; int main() { int n; double a[20][2]; cin >> n; for (int i=0;i<n;i++) { cin >> a[i][1] >> a[i][2]; } double x = a[0][2] / a[0][1] * 100; for(int i=1;i<n;i++){ double y = a[i][2] / a[i][1] * 100; if (y-x>5) { cout << "better" << endl; }else if (x-y>5){ cout << "worse" << endl; }else{ cout << "same" << endl; } } return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { int n; double a,b,c,t=0; cin >> n; for(int i=0;i<n;i++) { cin >> a >> b >> c; t = t + sqrt(a*a+b*b) * 2 / 50 + 1.5 * c; } printf("%.0f",ceil(t)); return 0; }
#include <iostream> using namespace std; int main() { long p,a,r =0,sum=0; for(int i=0;i<12;i++) { r = r + 300; cin >> p; r = r - p; a = r/100; sum = sum + a * 100; r = r - a * 100; if (r<0){ cout << "-" << i+1 << endl; break; } } if (r>0) cout << sum * 1.2 + r << endl; return 0; }
#include <iostream> using namespace std; int main() { long m,n,a,sum=0; cin >> m >>n; for(int i=0;i<n;i++) { cin >> a; if (m -a >= 0) m = m - a; else sum = sum + 1; } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main() { long n,a,b,sum=0,max=0; cin >> n; for(int i=0;i<n;i++) { cin >> a >> b; if (a>=90 && a<=140 && b >=60 && b <=90) { sum = sum + 1; }else{ if (sum > max) max = sum; sum = 0; } } if (sum > max) max = sum; cout << max << endl; return 0; }
#include <iostream> using namespace std; int main() { int i,j,k; bool flag = false; for(i=0;i<7;i++) { for(j=0;j<7;j++) { for(k=0;k<7;k++){ if (i*9*9+j*9+k==i+j*7+k*7*7 && (i!=0 || j!=0 || k!=0)) { cout << i*9*9+j*9+k << endl; cout << k << j << i << endl; cout << i << j << k << endl; } } } } return 0; }
#include <iostream> using namespace std; int main() { int n,a,sum=0; int first,second,third,forth; cin >> n; for(int i=0;i<n;i++) { cin >> a; forth = a % 10; third = (a - forth)/10 % 10; second = (a-forth - third * 10)/100 % 10; first = (a - forth - third*10 - second * 100)/1000; if (forth - first - second - third > 0) { sum = sum + 1; } } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main() { long N,M; bool light = true; cin >> N >> M; cout << 1 ; for(int i=2;i<=N;i++) { light = true; for(int j=1;j<=M;j++) { if(i%j==0) { light = ! light; } } if(!light) cout << "," << i; } return 0; }
#include <iostream> using namespace std; int main() { int n; double p=1,q=2,sum = 0,temp; cin >> n; for (int i=0;i<n;i++) { sum = sum + q / p; temp = q; q = q + p; p = temp; } printf("%.4f",sum); return 0; }
#include <iostream> using namespace std; int main() { int n; double sum = 0; cin >> n; for (int i=1;i<=n;i++) { if(i%2==1) sum = sum + 1.0d/i; else sum = sum - 1.0d/i; } printf("%.4f",sum); return 0; }
#include <iostream> using namespace std; int main(){ int a,b,c; cin >> a >> b >> c; for(int i=2;i<1000000;i++){ int t = a%i; if(b%i==t && c%i==t){ cout << i << endl; break; } } return 0; }
#include <iostream> using namespace std; int main(){ int n,sum=0; cin >> n; for(int i=1;i<=n;i++){ sum = sum + i; } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main(){ int a,b,n; cin >> a >> b >> n; for(int i=1;i<=n;i++){ a = a%b; a = a*10; } cout << a/b << endl; return 0; }
#include <iostream> #include <cstring> using namespace std; string wk[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; int main(){ int a,b,t=1; cin >> a >> b; for(int i=1;i<=b;i++){ t = t * a ; t = t % 7; } cout << wk[t] << endl; return 0; }
#include <iostream> using namespace std; int main(){ int a,b,t=1; cin >> a >> b; for(int i=1;i<=b;i++){ t = t * a; t = t % 1000; } if(t<10) cout << "00" << t << endl; else if(t<100) cout << "0" << t << endl; else cout << t << endl; return 0; }
求s=1+2+3+...+n,当加到第几项时,s的值会超过1000?
#include <iostream> using namespace std; int main(){ int n=0,s=0; while(s<=1000){ n++; s+=n; } cout << n << endl; return 0; }
求两个正整数m,n的最大公约数。
#include <iostream> using namespace std; int main(){ int m,n,gcd; cin >> n >> m; gcd = m>n?n:m; while(gcd>1 && (m%gcd!=0||n%gcd!=0)) gcd--; cout << gcd << endl; return 0; }
#include <iostream> using namespace std; int main(){ int m,n; cin >> n >> m; int r=n % m; while(r!=0){ n = m; m = r; r = n % m; } cout << m << endl; return 0; }
编一程序求满足不等式1+1/2+1/3+...+1/n>=5的最小n值。
#include <iostream> using namespace std; int main(){ int i=0; float s = 0; while(s<5){ ++i; s+=1.0/i; } cout << i << endl; return 0; }
#include <iostream> using namespace std; int main(){ int i; float s=0; for(i=1;s<5;i++){ s=s+1.0/i; } cout << i-1 << endl; return 0; }
输入一个整数,求出他们的最小值,最大值和平均值(保留3位小数)。输入保证这些数都是不超过1000的整数。
输入样例:2 8 3 5 1 7 3 6
输出样例:1 8 4.375
#include <iostream> using namespace std; int main(){ int x,n=0,min=10000,max=0,s=0; while(scanf("%d",&x)==1){ s+=x; min = x<min?x:min; max = x>max?x:max; n++; } printf("%d %d %.3lf",min,max,(double)s/n); return 0; }
#include <iostream> #define INF 100000000 using namespace std; int main(){ int x,n=0,min=INF,max=-INF,s=0; while(scanf("%d",&x)!=EOF){ s+=x; min = x<min?x:min; max = x>max?x:max; n++; } printf("%d %d %.3lf",min,max,(double)s/n); return 0; }
对于求两个正整数m,n的最大公约数可以用do-while实现。
#include <iostream> using namespace std; int main(){ int m,n,r; cin >> m >> n; do{ r = m % n; m = n; n = r; }while(r!=0); cout << m << endl; return 0; }
求1992个1992的乘积的末两位数是多少?
#include <iostream> using namespace std; int main(){ int a=1,t=0; do{ ++t; a=(a*1992)%100; }while(t!=1992); cout << a << endl; return 0; }
校体操队到操场集合,排成每行2人,最后多出1人;排成每行3人,也多出1人,分别按每行排4,5,6人,都多出1人;当排成每行7人时,正好不多。求校体操队至少多少人?
#include <iostream> using namespace std; int main(){ int x = 0; bool yes; do{ yes = true; x+=7; if(x%2!=1) yes = false; if(x%3!=1) yes = false; if(x%4!=1) yes = false; if(x%5!=1) yes = false; if(x%6!=1) yes = false; }while(yes==false); cout << x << endl; return 0; }
#include <iostream> using namespace std; int main() { double h,sum=0; cin >> h; sum = h; for(int i=1;i<10;i++) { sum = sum + h; h = h / 2; } cout << sum << endl; cout << h/2 << endl; return 0; }
#include <iostream> using namespace std; int main() { long long int N; cin >> N; while(N>1) { if(N%2==1) { cout << N << "*3+1=" << N * 3 + 1 << endl; N = N * 3 + 1; }else{ cout << N << "/2=" << N / 2 << endl; N = N / 2; } } cout << "End"; return 0; }
#include <iostream> using namespace std; int main() { long K,n=0; double sn = 0; cin >> K; while(sn<=K) { n = n + 1; sn = sn + 1.0d/n; } cout << n << endl; return 0; }
#include <iostream> using namespace std; int main() { long long int n,r; cin >> n; while(n>0) { r = n % 10; cout << r; n = (n - r)/10; if (n>0) { cout << " "; } } cout << endl; return 0; }
#include <iostream> using namespace std; int main() { long long int n,i=0; cin >> n; while(n!=0) { i = i*10 + n % 10; n = n / 10; } printf("%d",i); return 0; }
#include <iostream> using namespace std; int main() { long m,k,sum=0; cin >> m >> k; if (m % 19 ==0) { while(m!=0) { if (m % 10==3) { sum = sum + 1; } m = m / 10; } if (sum==k) cout << "YES" << endl; else cout << "NO" << endl; } else cout << "NO" << endl; return 0; }
求s=1!+2!+3!+...+10!
#include <iostream> using namespace std; int main(){ int s=0; for(int i=1;i<=10;i++){ int t=1; for(int j=1;j<=i;j++) t=t*j; s+=t; } cout << s << endl; return 0; }
#include <iostream> using namespace std; int main(){ int t=1,s=0; for(int i=1;i<=10;i++){ t*=i; s+=t; } cout << s << endl; return 0; }
对于给定的自然数n(n<20),在屏幕上输出仅由"*"构成的n行的直角三角形。
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ cout << "*"; } cout << endl; } return 0; }
百钱买百鸡问题。鸡翁一,值钱五,鸡母一,值钱三,鸡雏三,值钱一,百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
#include <iostream> using namespace std; int main(){ int x,y,z; for(x=0;x<=100/5;x++){ for(y=0;y<=100/3;y++){ for(z=0;z<=3*100;z++){ if(5*x+3*y+z/3==100 && x+y+z==100&&z%3==0) cout << x << " " << y << " " << z << endl; } } } return 0; }
#include <iostream> using namespace std; int main(){ int x,y,z; for(x=0;x<=100/5;x++){ for(y=0;y<=100/3;y++){ z = 100 -x -y; if(5*x+3*y+z/3==100 && z%3==0) cout << x << " " << y << " " << z << endl; } } return 0; }
求100~999中的水仙花数。若三位数ABC,ABC=A3+B3+C3,则称ABC为水仙花数。
例如153,13+23+33=1+125+27=153,则153是水仙花数。
#include <iostream> #include <iomanip> using namespace std; int main(){ for(int a=1;a<=9;a++){ for(int b=0;b<=9;b++){ for(int c=0;c<=9;c++){ if(a*a*a+b*b*b+c*c*c==a*100+b*10+c) cout << setw(6) << a*100+b*10+c; } } } return 0; }
#include <iostream> #include <iomanip> using namespace std; int main(){ for(int m=100;m<=999;m++){ int a=m/100; int b=(m%100)/10; int c = m%10; if(a*a*a+b*b*b+c*c*c==m) cout << setw(6) << m; } cout << endl; return 0; }
输出100~200中所有的素数
#include <iostream> #include <cmath> using namespace std; int main(){ for(int i=100;i<=200;i++){ int x=2; while(x<=floor(sqrt(i))&&(i%x!=0)) x++; if(x>floor(sqrt(i))) cout << i << "\t"; } cout << endl; return 0; }
输出所有形如aabb的四位完全平方数(即前两位数字相等,后两位数字也相等)。
#include <iostream> #include <cmath> using namespace std; int main(){ for(int i=1;i<=9;i++){ for(int j=0;j<=9;j++){ int n=i*1000+i*100+j*10+j; int m = sqrt(n); if(n==m*m) cout << n << endl; } } return 0; }
#include <iostream> using namespace std; int main(){ for(int x=32;;x++){ int n=x*x; if(n<1000) continue; if(n>9999) break; int hi=n/100,lo=n%100; if(hi/10==hi%10 && lo/10==lo%10) cout << n << endl; } return 0; }
把一个合数分解成若干个质因数乘积的形式(即求质因数的过程)叫做分解质因数。
分解质因数(也称分解素因素)只针对合数。
#include <iostream> using namespace std; int main(){ int n,i=2; cin >> n; cout << n << "="; do{ while(n%i==0){ cout << i; n/=i; if(n!=1) cout << "*"; } i++; }while(n!=1); cout <<endl; return 0; }
输入n,计算s=1!+2!+3!+...+n!的末6位(不含前导0)。n<=106,n!表示前n个正整数之积。
#include <iostream> using namespace std; int main(){ int n,s=0; cin >> n; for(int i=1;i<=n;i++){ int factorial=1; for(int j=1;j<=i;j++){ factorial*=j; } s += factorial; } cout << s%1000000 << endl; return 0; }
#include <iostream> #include <ctime> using namespace std; int main(){ const int MOD = 1000000; int n,s=0; cin >> n; for(int i=1;i<=n;i++){ int factorial = 1; for(int j=1;j<=i;j++){ factorial = (factorial * j )% MOD; s = (s+factorial) % MOD; } } cout << s << endl; printf("Time used = %.2lf\n",(double)clock()/CLOCKS_PER_SEC); return 0; }
#include <iostream> using namespace std; int main() { int n; long long int sum = 0,product=1; cin >> n; for (int i=1;i<=n;i++) { product = 1; for(int j=1;j<=i;j++) { product = product * j; } sum = sum + product; } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main(){ int n; double e = 1,p; cin >> n; for (int i=1;i<=n;i++){ p = 1; for(int j=1;j<=i;j++) { p = p * j; } e = e + 1/p; } printf("%.10f",e); }
#include <iostream> #include <cmath> using namespace std; int main(){ long int n; double x,f = 1,p; cin >> x >> n ; f = f + x; for (int i=2;i<=n;i++){ f = f + pow(x,i); } printf("%.2f",f); }
#include <iostream> using namespace std; int main(){ int M,N,X; cin >> M >> N >> X ; while(N <=M && X * N >=M) { if (M%N==0) { X = X - M / N; } else { X = X - M / N -1; } N = N + M/N; } printf("%d",N); return 0; }
#include <iostream> using namespace std; int main(){ int n,c[100]; cin >> n ; for(int i=n;i>=0;i--){ cin >> c[i]; c[i] = c[i] * i; } cout << c[n]; for(int i=n-1;i>0;i--) { cout << " " << c[i]; } cout << endl; return 0; }
#include <iostream> using namespace std; int main(){ int n,sum=0,a; cin >> n ; for(int i=1;i<=n;i++){ if(i%7!=0){ a = i; while(a>0) { if (a % 10 == 7) { break; } a = a / 10; } if (a ==0){ sum = sum + i * i; } } } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main(){ int n,sum=0,a; cin >> n ; for(int i=1;i<=n;i++){ a = i; while(a>0) { if (a % 10 == 1) { sum = sum + 1; } a = a / 10; } } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main(){ int L,R,sum=0,a; cin >> L >> R ; for(int i=L;i<=R;i++){ a = i; while(a>0) { if (a % 10 == 2) { sum = sum + 1; } a = a / 10; } } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main(){ int h,w,fill; char c; cin >> h >> w >> c >> fill ; for (int i=0;i<w;i++) cout << c; cout << endl; for (int i=0;i<h-2;i++){ cout << c; for(int j=0;j<w-2;j++) if (fill==0) cout << " "; else cout << c; cout << c << endl; } for (int i=0;i<w;i++) cout << c; cout << endl; return 0; }
#include <iostream> using namespace std; int main(){ long long int n,j,t; cin >> n ; for (int i=2;i*i<n;i++){ if(n % i ==0 ){ cout << n / i << endl; break; } } return 0; }
#include <iostream> #include <cmath> using namespace std; int main(){ long n,c=2,i=3,j; bool flag = false; cin >> n ; if (n==1) i = 2; else while(c<n) { i = i + 2; flag = true; for(j=2;j<=sqrt(i);j++) if (i % j==0) { flag = false; break; } if(flag) { c = c + 1; } } cout << i << endl; return 0; }
#include <iostream> using namespace std; int main(){ long n,coin=1,sum=0; cin >> n ; int count = 1; while(count <= n) { for(int j=1;j<=coin&&count <=n;j++){ sum = sum + coin; count = count + 1; } coin = coin + 1; } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main(){ int a ,b ,c ,ans=0; cin >> a >> b >> c; for(int i=0;i<=c/a;i++) for(int j=0;j<=c/b;j++) if(a*i+b*j==c) ans = ans + 1; cout << ans << endl; return 0; }