C++循环结构

上传人:206****923 文档编号:88626181 上传时间:2019-05-05 格式:DOC 页数:22 大小:204KB
返回 下载 相关 举报
C++循环结构_第1页
第1页 / 共22页
C++循环结构_第2页
第2页 / 共22页
C++循环结构_第3页
第3页 / 共22页
C++循环结构_第4页
第4页 / 共22页
C++循环结构_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《C++循环结构》由会员分享,可在线阅读,更多相关《C++循环结构(22页珍藏版)》请在金锄头文库上搜索。

1、1.公鸡6元一只,母鸡5元一只,小鸡1元三只,刚好花100元买100只鸡,有哪些买法?输入格式要求:无输出结果格式要求: 公鸡数 = 母鸡数 = 小鸡数 = #include using namespace std;int main()int g, m, x;for (g = 0; g = 20; g+)for (m = 0; m = 33; m+)x = 100 - g - m;if (!(x % 3) & x / 3 + m * 5+ g * 6 = 100)cout公鸡数 = gendl;cout母鸡数 = mendl;cout小鸡数 = xendl;return 0;2. 求多项式 1

2、!+2!+3!+15!的值。输出格式要求:couts=sendl;#include using namespace std;int main( )int s = 0;for (int i = 1; i = 15; i+)int x = 1;for (int j = 1; j = i; j+)x = x * j;s = s + x;cout s= s endl;return 0;3. 用“辗转相除方法”计算两个数 x,y 的最大公约数。输入格式要求:无,直接输入输出格式要求:无,直接输出结果#include using namespace std;int main()int x, y, n;ci

3、nxy;n = x % y;while (n != 0)x = y;y = n;n = x % y;coutyendl;return 0;4用while循环编程,求自然数1至100之间各奇数平方和sum。输出结果格式为:sum=166650#includeusing namespace std;int main ()int i = 1,n, sum=0;while (i = 100)n= i*i;sum = sum + n;i=i+2;cout sum= sumendl;return 0;5. 输出100到1000之间的各位数字之和能被15整除的所有数,输出时每10个一行。输入格式要求:无输出

4、格式要求:cout setw(5) m; 每10个一行。#include#includeusing namespace std;int main()int m, n, k, i = 0;for (m = 100; m 0);if (k % 15 = 0)cout setw(5) m;i+;if (i % 10 = 0) cout endl;return 0;6. 将1-100之间奇数顺序累加存入n中,直到其和等于或大于200为止。输出结果格式:n=225#include using namespace std;int main()int a,n;n=0;for(a=1;a=200)break;

5、a+=2;coutn=nendl;7. 有递推公式 f(n)=0.5*f(n-1)+0.7*f(n-2),已知f(0)=1,f(1)=1,求f(20)输入格式要求:无输出结果格式要求: f(20) = #include using namespace std;int main( )double f, f1, f2;f2 = 1;f1 = 1;for (int n = 2; n = 20; n+)f = 0.5 * f1 + 0.7 * f2;f2 = f1;f1 = f;cout f(20) = f endl;return 0;8. 求1至200之间的所有质数,将质数和存入变量 sum 中并输

6、出。质数(素数)的说明:“质数是只能被1和其本身整除的数”。输入提示要求:无输出结果格式要求:质数之间以一个空格隔开输出所有质数后换行输出:sum=4227# include # include using namespace std;int main()int N, m, sum = 0;for (m = 2; m = 200; m+)int i, tmp = (int)sqrt(m);for (i = 2; i tmp)cout m ;sum += m;coutendl;coutsum=sumendl;return 0;9. 以下程序的功能是以每行5个数来输出300以内能被7或17整除的偶

7、数,并求出其和。输入格式:无输出格式:每个数据以一个空格隔开,5个一行 和另起一行输出 cout sum= sumendl;#includeusing namespace std;int main()int i, n, sum;sum = 0;n = 0;for (i = 1; i 300 ; i+)if (i % 7 = 0 | i % 17 = 0)if (i % 2 = 0)sum = sum + i;n+;cout i ;if (n % 5 = 0) cout endl;coutendl;cout sum= sumendl;return 0;10从键盘上输入若干名学生的成绩,以一个负分

8、结束输入,计算平均成绩并统计90分以上的学生人数。输入信息格式要求:cout请输入学生成绩,以负数结束输入:endl;输出信息格式要求:输出平均成绩和90分以上的学生人数,中间用空格分隔,输出结束后换行结束程序#includeusing namespace std;int main()int score, n = 0, t = 0;double sum = 0, average;cout 请输入学生成绩,以负数结束输入: score;while (score = 0)if (score = 90)t = t + 1;sum = sum + score;n = n + 1;cin score;i

9、f (n 0)average = sum / n;cout average t endl;return 0;11. 从键盘输入一批非零整数,输出其中的偶数、奇数的平均值,用零作为终止标记。输入格式要求:无输出提示信息格式要求: 偶数平均值: 奇数平均值:#includeusing namespace std;int main()int x, i = 0, j = 0;float s1 = 0, s2 = 0, av1, av2;cin x;while (x != 0)if (x % 2 = 0)s1 = s1 + x;i+;elses2 = s2 + x;j+;cin x;if (i != 0) av1 = s1 / i;else av1 = 0;if (j != 0) av2 = s2 / j;else av2 = 0;cout 偶数平均值: av1 endl;

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

当前位置:首页 > 中学教育 > 其它中学文档

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