50道C编程练习题及解答
50 道 C/C++编程练习题 1、输入 3 个数,求最大值 int main() { int a,b,c,m; cinabc; m=a; if(bm) m=b; if(cm) m=c; coutabc; if(a==0) if(b==0) cout a; if(a=90) coutabc; if(a+bc else couta; if(amax) max=a; if(an; cina; m=a; s=a; for(int i=1; ia; s +=a; if(am) m=a; } cout n; sum = 0; max = n; while( n != -999 ) { sum = sum + n; if( n max ) max = n; count++; cin n; } if( count != 0 ) { ave=static_cast(sum) / count; cout1e-18) { pi=pi+t; i=i+1; j=j+2; t=t*i/j; } cout a; if(a0) n++; else if(aa; while(a!=0) { if(a%2 == 0) n += a;; else m += a; cin a; } cout x; s = s + x; if(xmax) max = x; } s = s - min - max; cout yearmonthday; for(i=1; i2) s++; cout z; while( z != 0 ) { while( z != x+y ) { coutz; } coutz; } } 49、从 52 个数里选 13 个数 int main() { int i,k,a[52],b[13]; for(i=0; i52; i++) a[i]=i+1; srand(time(0)); for(i=0; i13; i++) { k = rand() % (52-i); b[i] = a[k]; swap(a[k],a[51-i]); } for(i=0; i13; i++) coutb[i]“ “; } 50、求 100! const long MOD = 10000; int main() { int t,t0=time(0); int len,n=100000; unsigned long a[200000]; a[1]=1; len=1; for(int k=2; k=n; k++) { long carry=0; for(int i=1; i 0) { len++; a[len] = carry % MOD; carry = carry / MOD; } } t=time(0); int w=(len-1)*4 + int(log10(double(a[len]))) + 1; ofstream fout(“factorial.txt“); foutn“! = \n“a[len]; fout=1;i--) foutsetw(4)a[i]; foutendl; fout“用时:“t-t0“秒“endl; fout“数组元素个数: “len“阶乘值位 数:“wendl; return 0; } 8