C++编程题目算法大全.doc

上传人:新** 文档编号:562565779 上传时间:2023-12-15 格式:DOC 页数:64 大小:261.50KB
返回 下载 相关 举报
C++编程题目算法大全.doc_第1页
第1页 / 共64页
C++编程题目算法大全.doc_第2页
第2页 / 共64页
C++编程题目算法大全.doc_第3页
第3页 / 共64页
C++编程题目算法大全.doc_第4页
第4页 / 共64页
C++编程题目算法大全.doc_第5页
第5页 / 共64页
点击查看更多>>
资源描述

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

1、曾经在软通动力写过的算法题其中有一小部分是参考网上的资料,现在拿出来给大家分享! 第3章 控制语句 /* 1、打印出所有的“水仙花数”。所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如,153是一水仙花数,因为153 = 13 + 53 + 33。 */ #include void main() int i, a=0, b=0, c=0; for(i=100;i1000;i+) a=i%10; b=i/10%10; c=i/100%10; if(a*a*a+b*b*b+c*c*c=i) couti=iendl; /* 2、一个数如果恰好等于它的因子之和,这个数就称为“完数”。

2、例如,6的因子为1、2、3,而6 = 1 + 2 + 3,因此6是“完数”。编程序找出1000之内的所有完数,并按下面的格式输出其因子: 6 -1,2,3 */ #include void main() int i,j,sum=0,a50,k,t; for(i=1;i=1000;i+) sum=0; for(j=1;ji;j+) if(i%j=0) sum+=j; ak+=j; t=k; if(sum=i) couti; for(k=0;kt;k+) coutak; if(kt-1)cout,; coutendl; k=0; /* 3、求Sn=a+aa+aaa+aaa之值,其中a是一个数字。例

3、如:2+22+222+22222(此时n=5),n由键盘输入。*/ #include void main() double a,sn=0.0,sum=0.0; int n,i; couta; coutn; sn=a; sum=a; for(i=2;i=n;i+) sum=sum*10+a; sn+=sum; coutSn=snendl; /* 4、一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下。求它在第10次落地时,共经过了多少米?第10次反弹多高?*/ #include void main() double h1=100,h2=100,sum=0.0; int i; for

4、(i=1;i=10;i+) sum+=h2; h1=h1/2.0; h2=h1*2; coutsum=sum h1=h1endl; /* 5、猴子吃桃问题。猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉了一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩一个桃子了。求第一天共摘了多少桃子。*/ #include void main() int number,i; number=1; for(i=10;i1;i-) number=(number+1)*2; coutnumber=numberendl; 8.程

5、序中使用流格式输入、输出,我们可以怎么做? 答:在程序的开头包含头文件iostream.h cin输入,cout输出。 例如: #include void main() int a; couta; couta的值为:aendl; 第4章 函数 /* 1、写一函数用“气泡法”对输入的10个字符按由小到大的顺序排列。*/ #include void main() int i,j,temp,a10; coutplease input ten numbers:n; for(i=0;iai; for(i=0;i10;i+) /每循环一次确定数组中一个数的位置 for(j=i+1;jaj) temp=aj

6、; aj=ai; ai=temp; coutresort result=; for(i=0;i10;i+) coutai 1) */ #include double fun (double,double); void main() double n,x,sum; coutinput n and xnx; sum=fun(n,x); coutPn(x)=sum1) return (2*n1-1)*x1*fun(n1-1,x1)-(n1-1)*fun(n1-2,x1)/n1; /* 3、编写一函数,由实参传来一字符串,统计此字符串中字母、数字、空格、和其它字符的个数,并在主函数中输入字符串以及输出

7、上述结果。 */ #include void judge(char a); void main() const int size=100; char asize; cin.getline(a,size); judge(a); void judge(char a100)/判断字符类型 int letter=0,number=0,others=0,i=0; while(ai!=0) if (ai=a&ai=A&ai=0 & ai=9) number+;/统计数字个数 else others+;/统计其他数个数 i+; coutletter=letter number=number others=o

8、thersendl; /* 4、给出年、月、日,计算该日是该年的第几天。 */ #include int lead(int); void main() int ly,year,month,date,i,sum=0; coutyearmonthdate; int a12=31,0,31,30,31,30,31,31,30,31,30,31; ly=lead(year); if (ly=1) a1=29;/366天 else a1=28;/365天 for(i=0;imonth-1;i+) /当前月之前所有月天数累加和 sum+=ai; sum+=date; /加上当前月天数 cout你输入的日期

9、是当年的第sum天; int lead(int y)/判断闰年 if(y%4=0&y%100!=0)|(y%400=0) return 1;/是闰年 else return 0;/不是闰年 /* 5、写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果,两个整数由键盘输入。 */ #include int cdivisor(int,int); int cmultiple(int,int,int); void main() int x,y,d,m; coutxy; d=cdivisor(x,y); m=cmultiple(x,y,d); coutcommon d

10、ivisor is dendlcommon multiple is mendl; int cdivisor(int x1,int y1)/最大公约数 int r,temp; if (x1y1) temp=x1; x1=y1; y1=temp; while(x1%y1)/当较大数除以较小数余数等于0时,较小数为最大公约数 r=x1%y1; x1=y1; y1=r; return y1; int cmultiple(int x2,int y2,int d1)/最小公倍数 return x2*y2/d1;/两数相乘结果除以它们的最大公约数为最小公倍数 /* 6、写一函数,将两个字符串连接。 */ #

11、include #include void main() const int size=100; char asize,bsize; coutinput two string:endl; cin.getline(a,size); cin.getline(b,size); strcat(a,b); couta=aendl; /* 7、写一函数,将一个字符串的元音字母复制到另一个字符串,然后输出。 */ #include #include void scpy(char *,char *); void main() const int size=100; char asize=Hello world; char bsize=Net; couta= ab=

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

最新文档


当前位置:首页 > 生活休闲 > 科普知识

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