C++程序大全.doc

上传人:灯火****19 文档编号:136109712 上传时间:2020-06-24 格式:DOC 页数:7 大小:28.50KB
返回 下载 相关 举报
C++程序大全.doc_第1页
第1页 / 共7页
C++程序大全.doc_第2页
第2页 / 共7页
C++程序大全.doc_第3页
第3页 / 共7页
C++程序大全.doc_第4页
第4页 / 共7页
C++程序大全.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《C++程序大全.doc》由会员分享,可在线阅读,更多相关《C++程序大全.doc(7页珍藏版)》请在金锄头文库上搜索。

1、程序填空题1、下列程序计算1000以内能被3整除的自然数之和。#include using namespace std;void main( ) int x=1, sum;sum=0_;while (true) if (x1000) break;if (x%3=0) sum+=x;x+;coutsumendl;2. 以下程序段的功能是判断m是否为素数,请将程序补充完整。#include#includeusing namespace std;void main() int m,i,k; cinm; k=sqrt(float(m); for(i=2;ik) coutd” is a prime nu

2、mbern”mendl; else coutd” is not a prime numbern”mendl;三、写出程序的运行结果1. 假定输入10个整数:32,64,53,87,54,32,98,56,98,83。下列程序的输出结果是?#include using namespace std;void main( ) int a,b,c,x;a=b=c=0;for (int k=0; kx;switch(x%3) case 0: a+=x; break;case 1: b+=x; break;case 2: c+=x; break;couta”,”b”,”cendl;结果:141,64,45

3、22. 写出程序的运行结果#include using namespace std;void main( ) int j,k;for (j=5; j0; j-) for (k=j; k0; k-)cout”*”;coutendl;结果:*编程题1、 编写程序,求解方程ax2+bx+c=0的根。#include #include using namespace std;void main()int a,b,c;float x1,x2,z;cinabc;z=b*b-4*a*c;if(z0)x1=(-b)+sqrt(z)/(2*a);x2=(-b)-sqrt(z)/(2*a);coutThe res

4、ult: x1=x1 x2=x2endl;elseif(z=0)x1=-b/(2*a);coutThe result: x1=x1endl;elsecoutno result;2、 编写程序输出所有的水仙花数。所谓水仙花数是指一个三位数,其各位数的立方和等于该数。例如:153=13+53+33。#include using namespace std;void main()int a,b,c;for(int i=100;i=999;i+)a=i/100;b=i%100/10;c=i%10;if(a*a*a+b*b*b+c*c*c=i)coutiendl;3、 编写程序,计算s=1+(1+2)+

5、(1+2+3)+(1+2+3+n)的值。#include using namespace std;void main()int n,s,sum=0;cinn;for(int i=1;i=n;i+)s=0;for(int j=1;j=i;j+)s+=j;sum+=s;coutsumendl;第4章 数组和字符串一、写出程序的运行结果1#includeusing namespace std;void main() char s180,s240;int i=0,j=0;cins1;cins2;while(s1i!=0) i+;while(s2j!=0) s1i+=s2j+;s1i=0;cout“Th

6、e new string is : ”s1;答案( 将两个字符串首尾相连 )2. #includeusing namespace std;void main() int max_value(int array 4);int a34=1,3,5,7,2,4,6,8,15,17,34,12;coutmax_value(a);int max_value(int array 4) int i,j,max;max=array00;for(i=0;i3;i+) for(j=0;jmax) max=arrayij;return(max);答案( 求数组中的最大值 )3. #include#include u

7、sing namespace std;void main() char string81; int i,num=0,word=0;char c;gets(string);for(i=0;(c=stringi)!=0;i+) if(c= ) word=0; else if(word=0) word=1; num+; cout“there are words in the line.”num;程序运行中输入:I am a boy.(表示回车)答案( there are words in the line.4 )4. #includeusing namespace std;void main() i

8、nt a33,sum=0; int i,j; for(i=0;i3;i+) for(j=0;jaij; for(i=0;i3;i+) sum=sum+aii; cout“sum=”sumendl;程序运行中输入:1 2 3 4 5 6 7 8 9 (表示回车)答案( 15 对角线元素的和 )第6章 函数二. 读下列程序,写出运行结果1.#include using namespace std;int add(int x,int y)cout”In add(),received”x”and”yendl;cout”and return”x+yendl;return x+y;void main()i

9、nt a,b,c;coutab;cout”nCalling add():n”;c=add(a,b);cout”nBack in main():n”;cout”c was set to “cendl;cout”nExit n”;2. #include using namespace std; void add_1(int x) x+; void add_2(int &x) x+; void add_3(int *p) (*p+; void main() int a=2; add_1(a);coutaendl; 输出的结果为:_2_add_2(a);coutaendl; 输出的结果为:_3_add

10、_3(&a);coutaendl; /输出的结果为:_4_3. #include using namespace std; const int n=6; int fun(int k) int result;if(k=1) result=1;else result=2*fun(k-1);return result;void main( ) int a=3,res=0; res=n/a;coutresendl; /输出结果为:_2_res=fun(a);coutresendl; /输出结果为:_4_4. 下面程序的输出结果是( 14 )#include using namespace std;in

11、t i = 0;int fun(int n) static int a = 2;a+;return a+n;void main()int k = 5;int i = 2;k += fun(i);k += fun(i);cout k;5. 以下程序的输出结果是:(3 )#include using namespace std;int fun(char *s) char *p=s;while (*p!=0) p+;return (p-s);void main()coutfun(abc)endl;6#include using namespace std; int f(int a) return +a; int g(int& a) return +a; void main() int m=0,n=0; m+=f(g(m); n+=f(f(n); coutm=mend

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > IT计算机/网络 > 其它相关文档

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