为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

C++编程练习题及代码

2018-01-16 18页 doc 36KB 147阅读

用户头像

is_624976

暂无简介

举报
C++编程练习题及代码C++编程练习题及代码 5000 /*Problem Description 判断一个数是否为素数 Input 输入数据首先包含一个整数T,表示测试实例的个数,然后是T组测试数据。每组测试数据包含一个正整数n. (n>1) Output 如果n为素数则输出yes 反之输出no*/ #include using namespace std; int main() { int T,c,n; cin>>T; for(int i=0;i>n; for(c=2; c using namespace std; i...
C++编程练习题及代码
C++编程练习题及代码 5000 /*Problem Description 判断一个数是否为素数 Input 输入数据首先包含一个整数T,表示测试实例的个数,然后是T组测试数据。每组测试数据包含一个正整数n. (n>1) Output 如果n为素数则输出yes 反之输出no*/ #include using namespace std; int main() { int T,c,n; cin>>T; for(int i=0;i>n; for(c=2; c using namespace std; int main() { int i,sum=0,n; cin>>n; for (i=1;i<=n;i++) sum=sum+i; printf("The sum=%d\n",sum); return 0; } 6011 /*Problem Description 求两个正整数m和n的最大公约数 Input 测试数据有多组,处理到文件尾。每组测试数据在同一行输入两个正整数m和n。 Output 每组输出m和n的最大公约数。*/ #include #include using namespace std; int main() { 第 1 页 共 8 页 int m,n,x; while(cin>>m>>n) { if (m>n) x=n; else x=m; while(m%x>0||n%x>0) {x=x-1;} cout< #include using namespace std; int main() { double a,b,c,d,e,min,max; while(cin>>a>>b>>c>>d>>e) { min=a; max=a; if (bmax) max=b; if (c>max) max=c; if (d>max) max=d; if (e>max) max=e; printf("%.6lf ",max); printf("%.6lf\n",min); } return 0; } 6013 /*Problem Description 判断所输入年份是否为闰年。 Input 测试数据有多组,处理到文件尾。每组测试数据输入年份n。 Output 对于每组输入年份n,若n为闰年,输出is LEAP!,否则输出is NOT LEAP!。*/ #include #include using namespace std; int main() { int n; while(cin>>n) { if (n%4==0) cout<<"is LEAP!"<建议
使用double型),计算以这3个数为边长的三角形的面积。若这3个数不构成三角形,则不 输出。 Input 测试数据有多组,处理到文件尾。每组测试输入3个浮点数。 Output 每组输出以这3个数为边长的三角形的面积(保留6位小数)。*/ #include #include #include using namespace std; int main() { double a,b,c,p,s; while(cin>>a>>b>>c) { if (a+b>c && a+c>b &&b+c>a) { p=(a+b+c)/2; s=sqrt(p*(p-a)*(p-b)*(p-c)); printf("%.6lf\n",s); } } return 0; } 6015 /*Problem Description 输出形状为直角三角形的九九乘法表(每个元素占4个位置) Sample Output 1 2 3 4 5 6 7 8 9 1 1 2 2 4 3 3 6 9 4 4 8 12 16 5 5 10 15 20 25 6 6 12 18 24 30 36 7 7 14 21 28 35 42 49 8 8 16 24 32 40 48 56 64 9 9 18 27 36 45 54 63 72 81*/ #include #include #include using namespace std; int main() { int i,j,a,b,n; printf("%8d",1); for(i=2;i<=9;i++) printf("%4d",i); cout< #include #include using namespace std; int main() { double e,s,x;int m,i; while(cin>>s) { e=1; m=1; i=1;x=1.0/m; while(x>=s) { e=e+x; i=i+1; m=m*i; x=1.0/m; } e=e+x; printf("%.18lf\n",e); } return 0; } 6017 /*Problem Description 若一头小母牛从第4个年头开始每年生育一头母牛,按照此规律,第n年时有多少头母牛。 Input 测试数据有多组,处理到文件尾。每组输入第n年的母牛数。 Output 每组输出第n年时的母牛数。*/ #include using namespace std; int main() { int i,j,n,x; int a[1000]; while(cin>>n) { x=1; a[1]=1;a[2]=0;a[3]=0;a[4]=1; 第 4 页 共 8 页 if (n<4) cout< using namespace std; int main() { int n,x; while(cin>>n) { x=n*(n-1); cout< using namespace std; int main() { int a[100],b[100],c[100]; int i,j; for (i=0;i<=99;i++) { a[i]=i; for (j=0;j<=(100-a[i]*3)/2;j++) { b[j]=j; c[j]=100-a[i]-b[j]; if (a[i]*3+b[j]*2+c[j]/2==100 && c[j]%2==0) cout< using namespace std; int main() { int n,i,m,an; m=0; for (n=-39;n<=40;n++){ an=n*n-n+41; if (an%2==0) m=1; else for(i=3;i<=an/2+1;i++) if (an%i==0) m=1; } if (m==1) cout<<"NO"< using namespace std; int main() { int i,x; for (i=1;i<=1000;i++) { x=i*7; if (x%2==1 && x%3==2 && x%5==4 && x%6==5) break; } cout< #include using namespace std; int main() { int b[10]; int n,i,a,j,c,l,s; while(cin>>n>>a){ s=0; for (i=1;i<=n;i++) { b[i]=a;c=1; for (j=2;j<=i;j++) { c=c*10; b[i]=b[i]+a*c; } 第 6 页 共 8 页 s=s+b[i]; } cout< #include using namespace std; int main() { double b[100]; int n,i; double s; while(cin>>n){ s=0; for (i=1;i<=n;i++) { b[i]=i+i*i+1.0/i; s=s+b[i]; } printf("%.6lf\n",s); } return 0; } 6025 /*Problem Description 打印出所有的"水仙花数"。所谓"水仙花数"是指一个3位数,其各位数字的立方和等于该数本身。 例如,153是一水仙花数,因为153=1*1*1+5*5*5+3*3*3。*/ #include #include using namespace std; int main() { int a,b,c,i; for (a=1;a<=9;a++) for (b=0;b<=9;b++) for (c=0;c<=9;c++){ i=a*100+b*10+c; if (i==a*a*a+b*b*b+c*c*c) cout< #include using namespace std; int main() { int i,n,s; while(cin>>n) { s=1; for (i=n-1;i>=1;i--) s=(s+1)*2; printf("%d\n",s); } return 0; } 6032 /*Problem Description 输入边长n(n?20),用‘*’号打印边长为n的菱形(见Sample Input和Sample Output)。 Input 测试数据有多组,处理到文件尾。每组输入边长n。 Output 每组输出以n为边长的菱形。两行之间无空行,星号之间一空格。 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #include #include using namespace std; int main() { int i,j,n; while(cin>>n) { for (i=1;i<=n;i++) { for (j=1;j<=2*(n-i);j++) cout<<' '; cout<<'*'; for (j=2;j<=i;j++) cout<<' '<<'*'<<' '<<'*'; cout<=1;i--){ for (j=1;j<=2*(n-i);j++) cout<<' '; cout<<'*'; for (j=2;j<=i;j++) cout<<' '<<'*'<<' '<<'*'; cout<
/
本文档为【C++编程练习题及代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索