c++程序 源代码

上传人:ni****g 文档编号:490500637 上传时间:2022-12-20 格式:DOCX 页数:14 大小:20.80KB
返回 下载 相关 举报
c++程序 源代码_第1页
第1页 / 共14页
c++程序 源代码_第2页
第2页 / 共14页
c++程序 源代码_第3页
第3页 / 共14页
c++程序 源代码_第4页
第4页 / 共14页
c++程序 源代码_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《c++程序 源代码》由会员分享,可在线阅读,更多相关《c++程序 源代码(14页珍藏版)》请在金锄头文库上搜索。

1、2011-12-21 14:54C卄期末复习1. 题目: 求多项式 1!+2!+3!+15!的值并 存入变量 out 中.#include void main()int i,sum=1;int n=15,out=0; for(i=1;i=n;i+)sum=sum*i; out+=sum;cout1!+2!+15!=outendl;输出结果:sum 中.2. 题目:求 1 至 200之间的所有质数,将质数和存入变量 #include #include void main()int a,b,c;int add=0,i;for(a=2;a=200;a+)b=sqrt(a);for(c=2;cb)ad

2、d+=a;cout1200之间所有质数的和是:addendl;输出结果:1200之间所有质数的和是:42273. 题目:用 while 循环编程,求自然数 1 至 100 之间各奇数平方和 sum #include void main()int a=1,b;long int sum=0;while(a=99)b=a*a;sum+=b;a=a+2;cout1100所有奇数的平方和是:sumendl;输出结果: 1100所有奇数的平方和是:166650.4. 题目:判断一个数23437是否是素数(要求程序中设置一个参数flag,flag 为 1 代表是素数,为 0 代表不是)#include #i

3、nclude void main()int a=23437;int b,c,flag;b=sqrt(a);for(c=2;cb)flag=1;elseflag=0;if(flag=1)cout是素数! endl;else if(flag=0)cou t不是素数!endl;输出结果:不是素数!5. 题目:已知一个数m(=252)求各位数字之和void main()int m=252;int b,c,d;b=m/100;c=(m-100*b)/10;d=(m-100*b-10*c);cout各位数字之和是:b+c+dendl;输出结果:各位数字之和是: 96. 题目:将1-100之间奇数顺序累加存

4、入n中,直到其和等于或大于200为止 #include void main()int a,n;n=0;for(a=1;a=200)break;a+=2;coutnendl;输出结果: 2257. 题目: 用“辗转相除方法”计算两个数 x,y 的最大公约数#include void main()int x,y,q;cinxy;while(q=x%y)!=0)x=y;y=q;cout最大公约数是:yendl;输入: 15 9输出结果:最大公约数是: 38. 题目:已知三个数a, b, c,按由小到大的顺序存入a, b, c中并输出。#include void main()int a,b,c,q;

5、cinabc;if(ab) q=a;a=b;b=q;if(ac)q=a;a=c;c=q;if(bc)q=b;b=c;c=q; coutatbtcendl;输入: 99 22 11输出结果: 11 22 999. 题目:已知n,计算n以内的所有素数之和sum。#include #include void main()int n,m,a,c,sum=0;cinn;for(a=2;a=n;a+)m=sqrt(a);for(c=2;cm)sum=sum+a;coutn以内所有素数的和是:sumendl;输入: 10输出结果: 10以内所有素数的和是: 1710. 题目: 打印水仙花数及个数 n (水仙

6、花数是三位数,每位数字的立方和等 于这个数本身示例见图 1)。void main()int a,b,c,d,i=0;for(a=100;a=999;a+)b=a/100;c=(a-100*b)/10;d=a-100*b-10*c;if(b*b*b+c*c*c+d*d*d=a)coutaendl;i+;cout有i个水仙花数!endl;输出结果:153370371407有 4 个水仙花数!11. 题目:找出1-200之间的完数及个数n (完数是一个数的因子之和是这个 数本身。例如 6=1+2+3)。#include void main()int m,s,i;int n=0;for(m=2;m=2

7、00;m+)s=0;for(i=1;im;i+)if(m%i)=0)s=s+i;if(s=m)cout its factors are: mendl;n+;cout”有”n”个完数!” endl;输出结果:its factors are: 6its factors are: 28212. 题目:编写一个C+函数max,输入参数为3个实数,输出为其中的最大数。#include int max(int a,int b,int c)int q;if(ac)q=a;a=c;c=q;if(ab)q=a;a=b;b=q;if(bc)q=b;b=c;c=q;return(c);void main()int

8、a,b,c,q;cinabc;q=max(a,b,c);coutqendl;输入: 1 3 55输出结果: 5513. 题目:编写一个函数:int isSushu(int n),判断n是否是素数。#include #include int isSushu(int n)int q,w,e;q=sqrt(n);for(w=2;wq)e=1;elsee=0;return(e);void main()int n,q;cinn;q=isSushu(n);if (n=1)cou t不是素数!endl;else if(q=1)cou t是素数!endl;else if(q=0)cou t不是素数!endl;

9、输入:12输出结果:不是素数!14. 题目:从键盘中分别给两个整形变量x和y赋值为20和10,通过编程使它 们的值交换,然后输出交换以后的 x 和 y 的值。#include void main()int x,y,q;cinxy;coutxtyendl;q=x;x=y;y=q;coutxtyendl;输出: 20 10输出结果: 20 1010 2015. 题目: 利用 switch 语句将输入的百分制成绩转换成相应的等级。 成绩 等级10090 优秀8980 良好7970 中等6960 及格590 不及格#include void main()int w,fen;cinfen;if(fen1

10、00) cout”error”;elsew=fen/10;switch(w)case 10:cou t优秀!;break;case 9:cou t优秀!;break;case 8:co ut良好! ;break;case 7:co ut中等;break;case 6:co ut及格!;break;case 5:cou t不及格!;break;case 4:cou t不及格!;break;case 3:cou t不及格!;break;case 2:cou t不及格!;break;case 1:cou t不及格!;break;case 0:cou t不及格!;break;coutendl;输入:1

11、00输出结果:优秀16. 题目:输入一个自然数num,将该数的每一位数字按反序输出digital。 例如:输入123456,输出 654321。 (要求输入123456)#include void main()int a,b; cina; while(a0)b=a%10; coutb; a=(a-b)/10;coutendl; 输入:123456输出结果:65432117. 题目: 利用循环语句输出“九九乘法表”。#include void main()int i,j;for(i=1;i=9;i+)for(j=1;j=9;j+)couti*j=i*j;cout ;coutendl;输出结果:略!18. 题目: 利用 for 循环和 break 语句实现:从键盘上输入若干个(小于 10 个)正整数,直到输入负数为止,计算并输出已输入的正整数之和。 (要求输入:10, 3, 9, 27, 12, -5)#include void main()int i,a,add=0;for(i=1;ia;add+=a;if(a0)break;coutaddendl;输入:10 3 9 27 12 -5输出结果:5

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号