} else if(score
Please input the number of students: 5? input */
The score of student 1 is: 78? input */
The score of student 2 is: ? */
The score of student 3 is: 43? input */
The score of student 4 is: 97? input */
The score of student 5 is: 81? input */
The average score is: . The highest score is: . The lowest score is: .
The ratio of fails to passes is: 1: 6-23
/* Blue is /* Blue is
/* Blue is input
/* Blue is /* Blue is /* Blue is
输入n个正整数,输出其中偶数的平均数(结果四舍五入为整数),以及奇
数的个数。
Program #include <> main() { int i, n, integer, num_odd=0, sum=0; printf(\ scanf(\ for(i=1; i<=n; i++) { printf(\ scanf(\ while(integer<=0) { printf(\ printf(\ scanf(\ } if(integer%2==0) /*若求奇数的平均数如何修改?*/ sum += integer; else num_odd++; } printf(\ if (sum>0) printf(\ else printf(\ printf(\ } /*若要求将四舍五入的平均值保存到一个整型变量里,如何处理?*/ Output
Please input the number of positive integers: 5? input */
The positive integer 1 is: 12? input */
The positive integer 2 is: 13? input */
The positive integer 3 is: 24? input */
The positive integer 4 is: 25? input */
The positive integer 5 is: 30? input */
The average of the even integers is: 22. The number of the odd integers is: 2. 6-24 6-25 6-26 6-27
输入一个整数(长整型)
(1)统计该整数的位数。例如:输入17623,输出5。 (2)将其逆序输出。例如:输入17623,输出32671。
(提示:使用取模运算来抽取最后一个数字,将该整数除以10,就可
/* Blue is /* Blue is /* Blue is /* Blue is /* Blue is /* Blue is
以把n个数字的数变为n-1个数字的数值。) 6-28
(3)计算各位数字的和(如输入的整数为123,则各位数字的和为1+2+3=6)。
Program #include <> main() { } long n, m; int digit=1, sum=0; printf(\ scanf(\ if (n==0) printf(\ else { m=n; /*统计位数*/ while ((m/=10)!=0) digit++; printf(\ /*逆序输出*/ printf(\ if(n<0) { n=-1*n; printf(\ } /*负数是如何处理的?*/ /*计算各位数字之和*/ while(n!=0) { printf(\ sum += n; n/=10; } printf(\ } /*程序结束时,m是多少?n是多少?*/ Output
Please input an integers: 17623? */
The digit of 17623 is: 5. The reverse number is: 32671 The sum of each digit is: 19. 6-29
/* Blue is input
输出100以内的全部素数,每行输出5个。
Program #include <> #include <> main() { int m, i, k, n; for (m=2, n=0; m<=100; m++) { k=sqrt(m); /*可否写成k=m?会有什么问题?*/ for (i=2; i<=k; i++) /*若上句是k=m,如何修改条件?*/ } { if (m%i==0) break; } if (i>k) { printf(\ n=n+1; if (n%5==0) printf(\ } } /*每行输出5个素数*/ Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 6-30 6-31
将一个大于1的正整数分解成质因数,例如输入90,输出90=2*3*3*5。 (提示:不必判断因数是否为质数,如何做?)
Program #include <> main() { int m=2; long p; do { printf(\input a positive integer (greater than 1): \ scanf(\ if (p<=1) printf(\integer is less than 1! Please try again!\\n\ else break; } while (1); /*这个循环是在做什么?*/ printf(\ while(m<=p) { while (p%m!=0) m++; printf(\ /*为何不用判断m是否为素数?*/ p=p/m; } */ } printf(\/* '\\b' 后面有个空格。该语句起什么作用?Output
Please input a positive integer (greater than 1): 90? /* Blue is input */ 90=2*3*3*5 6-32
输出1~1000的所有“完数”。所谓“完数”是指该数等于其所有因子(除
了这个数本身)的和,例如6=1+2+3。
Program #include <> main() { int p, i, sum; for(p=2, i=1, sum=0; p<=1000; p++, i=1, sum=0) { while (i
6 6-33
28
496
打印出所有的“水仙花数”。所谓“水仙花数”是指一个3位数,其各位数
字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=13+53+33。 Program #include <> #include <> main() { int p, sum; for(p=100; p<=999; p++) { sum = pow(p,3) + pow(p/10,3) + pow(p/100,3); if (sum == p) printf(\ } } Output