C语言上机练习参考答案 下载本文

内容发布更新时间 : 2024/5/10 7:38:47星期一 下面是文章的全部内容请认真阅读。

6-34

153 370 371 407

找出1~99之间的全部“同构数”。所谓“同构数”是指这样的数:它出现

在其平方数的右边,例如:5出现在其平方数25的右边,25出现在其平方数625的右边,5和25都是同构数。

Program #include <> main() { int p, square, n; for(p=1; p<=99; p++) { n=p<10?10:100; /*n的作用是?*/ square=p*p; if (square%n==p) printf(\ } } Output

1 (1*1=1) 5 (5*5=25) 6 (6*6=36)

25 (25*25=625) 76 (76*76=5776) 6-35

百元买百鸡。已知公鸡5元一只,母鸡3元一只,小鸡1元三只。问用100

元买100只鸡,那么公鸡、母鸡和小鸡各多少只?(提示,不止一个解)。 Program #include <> main() { int cock, hen, chick; for(cock=0; cock<=100; cock++) { for(hen=0; hen<=100-cock; hen++) { chick=100-cock-hen; if (chick%3!=0) /*这个if语句是什么意思?*/ continue; if(cock*5+hen*3+chick/3==100) printf(\ } } } Output

Cock: 0, hen: 25, chick: 75 Cock: 4, hen: 18, chick:78 Cock: 8, hen: 11, chick:81 Cock: 12, hen: 4, chick:84 6-36

输入两个整数lower和upper,输出一张华氏-摄氏温度转换表,华氏温度

的取值范围是[lower, upper]。计算公式如下: 6-37 6-38 6-39 6-40

c?5?(f?32) 9式中:c表示摄氏温度,f表示华氏温度。 (1)表中每行的华氏温度依次递增1oF; (2)表中每行的华氏温度依次递增2oF。

Program (1) #include <> main() { int lower, upper, f; float c; do { printf(\(lower, upper): \ scanf(\ if(lower>upper) printf(\lower Fahrenheit degree is higher than the upper! Please try again!\ } while (lower>upper); /*这个do-while语句做什么用?*/ printf(\ for(f=lower; f<=upper; f++) { c=*(f-32)/9; printf(\ } } Output

Please input the range of Fahrenheit degree (lower, upper): 90, 105?

/* Blue is input

*/

Fahrenheit degree(F) Celsius degree(C) 90 91 92

93 94 95 96 97 98 99 100 101 102 103 104 105

/*如何实现100和99右对齐的?*/

Program (2) #include <> main() { int lower, upper, f; float c; do { printf(\(lower, upper): \ scanf(\ if(lower>upper) printf(\lower Fahrenheit degree is higher than the upper! Please try again!\ } while (lower>upper); printf(\ for(f=lower; f<=upper; f=f+2) /*如何实现递增2oF ?*/ { c=*(f-32)/9; printf(\ } } Output

Please input the range of Fahrenheit degree (lower, upper): 85, 90? /* Blue is input */

Fahrenheit degree(F) Celsius degree(C) 85 87 89 6-41 6-42

输入一个正整数n,输出一张2的乘方表,显示20~2n的值。 (1)调用pow( )函数实现(注意:pow函数的类型为double);

6-43 (2)不调用pow( )函数实现。

Program (1) #include <> #include <> main() { int n, i; printf(\ scanf(\ printf(\ for(i=0; i<=n; i++) printf(\ } /*为什么在pow(2,i)之前要用(long)?*/ Program (2) #include <> main() { int n, i; long p=1; printf(\ scanf(\ printf(\ printf(\/*为何有这行输出?能在for中实现吗?*/ for(i=1; i<=n; i++) { p = p*2; printf(\ } } Output

Please input n: 5? /* Blue is input */

Base Power Result 2 0 1 2 1 2 2 2 4 2 3 8

2 4 16 /*如何实现Result右对齐?*/ 2 5 32 6-44

输入一个正整数n(n<13),输出一张n的阶乘表,显示0!~n!的值;

Program (1) #include <> main() { } int n, i, j; long f; printf(\ scanf(\ for(i=0; i<=n; i++) { f=1; for(j=1;j<=i;j++) f=f*j; printf(\ } 思考:如果n>=13,输出的结果是否还正确?如果想得到正确的结果,如何修改程序? 提示:修改f的数据类型。(那么,输出该如何修改?见下面程序。)

Program (2) #include <> main() { int n, i, j; float f; /*更大的数字,可以用double*/ printf(\ scanf(\ for(i=0, f=1; i<=n; i++) { if(i>1) /*如果没有i>1的条件限制,会得到什么结果?*/ f = f*i; printf(\ } /*为何只有一个for循环?和上面的程序有何不同?*/ } Output

Please input n: 13? /* Blue is input */ 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 11! = 12! = 0

13! = 00 /*这是Program(2)的结果,Porgram(1)的输出结果是13!