《2009春学期C语言上机练习答案汇总(共62页).doc》由会员分享,可在线阅读,更多相关《2009春学期C语言上机练习答案汇总(共62页).doc(62页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上第2周(M2)20011求华氏温度100F对应的摄氏温度。计算公式如下,c=5*(f-32)/9,式中:c表示摄氏温度,f表示华氏温度。(源程序error02_1.cpp)输入输出示例:fahr=100,celsius=37#include int main(void) int celsius, fahr;/*-*/fahr=100;celsius=5*(fahr-32)/9; printf(fahr = %d, celsius = %dn, fahr, celsius); return 0;20012 求华氏温度 150F 对应的摄氏温度。计算公式:C = 5*F/
2、9-5*32/9,式中:C表示摄氏温度,F表示华氏温度。输入输出示例:fahr=150, celsius=66#include int main(void) int celsius, fahr;/*-*/fahr=150;celsius=5*fahr/9-5*32/9;printf(fahr = %d, celsius = %dn,fahr, celsius); return 0;20013求摄氏温度26C对应的华氏温度。计算公式如下,f=9*c/5+32,式中:c表示摄氏温度,f表示华氏温度。输入输出示例:celsius=26,fahr=78#include int main(void) i
3、nt celsius, fahr;/*-*/celsius=26;fahr=9*celsius/5+32; printf(celsius = %d, fahr = %dn, celsius, fahr);return 0;20015当n为152时,分别求出n的个位数字(digit1)、十位数字(digit2)和百位数字(digit3)的值。输入输出示例:整数152的个位数字是2,十位数字是5,百位数字是1#include int main(void) int n,digit1,digit2,digit3;n=152;digit1=n%10;digit2=(n/10)%10;digit3=n/1
4、00;/*-*/ printf(整数%d的个位数字是%d, 十位数字是%d, 百位数字是%dn, n, digit1, digit2, digit3);return 0;20026 输入2个整数 num1 和 num2,计算并输出它们的和、差、积、商与余数。输出两个整数的余数可以用 printf(%d % %d = %dn, num1, num2, num1%num2);输入输出示例:括号内是说明输入5 3 (num1=5,num2=3)输出5 + 3 = 85 - 3 = 25 * 3 = 155 / 3 = 15 % 3 = 2#include int main(void) int num
5、1,num2;/*-*/scanf(%d%d,&num1,&num2);printf(%d + %d = %dn,num1,num2,num1+num2);printf(%d - %d = %dn,num1,num2,num1-num2);printf(%d * %d = %dn,num1,num2,num1*num2);printf(%d / %d = %dn,num1,num2,num1/num2);printf(%d % % %d = %dn,num1,num2,num1%num2);return 0;第3周(M3)20031 求1+2+3+.+100(调试示例error02_5)计算表
6、达式 1 + 2 + 3 + . + 100的值。输出示例:sum = 5050#include int main(void) int i, sum; sum=0;for(i=1;i=100;i+)sum=sum+i; printf(sum = %dn, sum);20032 求m+(m+1)+(m+2)+.+100输入一个正整数repeat (0repeat10),做repeat次下列运算:输入一个正整数m(0=m=100),计算表达式 m + (m+1) + (m+2) + . + 100的值。输入输出示例:括号内为说明输入3(repeat=3)0(计算0+1+2+.+100)10(计算1
7、0+11+12+.+100)50(计算50+51+52+.+100)输出sum = 5050sum = 5005sum = 3825#include int main(void) int i, m, sum; int repeat, ri; scanf(%d, &repeat); for(ri = 1; ri = repeat; ri+) scanf(%d, &m);sum=0; for(i=m;i=100;i+)sum=sum+i;/*-*/ printf(sum = %dn, sum); 20033 求1/m1/(m+1)1/(m+2).1/n输入一个正整数repeat (0repeat1
8、0),做repeat次下列运算:输入2个正整数 m 和 n(m=n),计算表达式 1/m + 1/(m+1) + 1/(m+2) + . + 1/n的值,输出时保留3位小数。输入输出示例:括号内为说明输入3 (repeat=3)5 15 (计算1/5+1/6+1/7+.+1/15)10 20 (计算1/10+1/11+1/12+.+1/20)1 3 (计算1+1/2+1/3)输出sum = 1.235sum = 0.769sum = 1.833#include int main(void) int i, m, n; int repeat, ri; double sum; scanf(%d, &
9、repeat); for(ri = 1; ri = repeat; ri+) scanf(%d%d, &m, &n); sum=0;for (i=m; i=n; i+)sum=sum+1.0/i; printf(sum = %.3fn, sum); 20034 求1 + 1/3 + 1/5 + .的前n项和输入一个正整数repeat (0repeat10),做repeat次下列运算:输入一个正整数 n,计算表达式 1 + 1/3 + 1/5 + . 的前 n 项之和,输出时保留6位小数。输入输出示例:括号内为说明输入2(repeat=2)5(计算1+1/3+1/5+1/7+1/9)23(计算1
10、+1/3+1/5+.+1/45)输出sum = 1.sum = 2.#include int main(void) int i, n;int denominator; int repeat, ri; double sum; scanf(%d, &repeat); for(ri = 1; ri = repeat; ri+) scanf(%d, &n); sum=0;denominator=1;for(i=1;i=n;i+)sum=sum+1.0/denominator;denominator=denominator+2; printf(sum = %.6fn, sum); 20035 求11/4
11、1/71/10的前n项之和输入一个正整数repeat (0repeat10),做repeat次下列运算:读入一个正整数 n,计算11/41/71/10的前 n 项之和,输出时保留3位小数。输入输出示例:括号内是说明输入2 (repeat=2)310输出sum = 0.893sum = 0.819#include int main(void) int flag, i, n, t; int repeat, ri; double item, sum; scanf(%d, &repeat); for(ri = 1; ri = repeat; ri+) scanf(%d, &n); sum=0;flag
12、=1;t=1;for(i=1;i=n;i+)item=flag*1.0/t;sum=sum+item;flag=-flag;t=t+3;/*-*/ printf(sum = %.3fn, sum); 20036 输出华氏-摄氏温度转换表(改错题error02_6)输入一个正整数repeat (0repeat10),做repeat次下列运算:读入2个整数 lower 和 upper,输出一张华氏摄氏温度转换表,华氏温度的取值范围是lower, upper,每次增加2F。计算公式:c = 5 * (f - 32) / 9,其中:c表示摄氏温度,f表示华氏温度。输出请使用语句 printf(%3.0
13、f %6.1fn, fahr, celsius);输入输出示例:括号内是说明输入2 (repeat=2)32 35 (lower=32,upper=35)40 30 (lower=40,upper=30)输出fahr celsius 32 0.0 34 1.1fahr Celsius#include int main(void) int lower, upper; int repeat, ri; double celsius, fahr; scanf(%d, &repeat); for(ri = 1; ri = repeat; ri+) scanf(%d%d, &lower, &upper);
14、 printf(fahr celsiusn); for(fahr=lower;fahr=upper; fahr+, fahr+)celsius=5*(fahr-32)/9;printf(%3.0f%6.1fn,fahr,celsius);/*-*/20038 求x的n次幂输入一个正整数repeat (0repeat10),做repeat次下列运算:读入1 个实数x和正整数 n(n=50),计算并输出 x 的 n 次幂(保留2位小数),不允许调用pow函数求幂。输入输出示例:括号内是说明输入2 (repeat=2)1.5 2 (x=1.5,n=2)2 7 (x=2,n=7)输出2.25128.0
15、0#include int main(void) int i, n; int repeat, ri; double mypow, x; scanf(%d, &repeat); for(ri = 1; ri = repeat; ri+) scanf(%lf%d, &x, &n);mypow=1;for(i=1;i=n;i+)mypow=mypow*x;/*-*/ printf(%.2fn, mypow);20041 生成 3 的乘方表输入一个正整数n,生成一张3的乘方表,输出30 3n的值,可调用幂函数计算3的乘方。输出使用语句 printf(pow(3,%d) = %.0fn, i, mypo
16、w);输入输出示例:括号内是说明输入3(n=3)输出pow(3,0) = 1pow(3,1) = 3pow(3,2) = 9pow(3,3) = 27#include #include int main(void) int i, n; double mypow; scanf(%d, &n);for(i=0;i=n;i+) mypow=pow(3,i);printf(pow(3,%d) = %.0fn, i, mypow);20044 求1000.51010.510000.5计算 1000.51010.510000.5的值(保留2位小数),可调用sqrt函数计算平方根。输入输出示例:括号内是说明
17、输出sum = 20435.99#include #include int main(void) int i; double sum;sum=0;for(i=100;i=1000;i+) sum=sum+sqrt(i);/*-*/ printf(sum = %.2fn,sum);20053 计算物体自由下落的距离一个物体从 100m 的高空自由落下,编写程序,求它在前 3s 内下落的垂直距离(结果保留2位小数)。设重力加速度为10米/秒2。#include int main(void) double height; height=0.5 * 10 * 3* 3; /*-*/ printf(he
18、ight = %.2fn, height);20056 计算分段函数输入一个正整数repeat (0repeat=0 时,f(x) = x0.5,当 x小于0时,f(x) = x5 + 2x + 1/x。输入输出示例:括号内是说明输入3 (repeat=3)10 (x=10)-0.5 (x=-0.5)0 (x=0)输出f(10.00) = 3.16f(-0.50) = -3.03f(0.00) = 0.00#include #include int main(void) int repeat, ri; double x, y; scanf(%d, &repeat); for(ri = 1; r
19、i =0) y=sqrt(x);else y=pow(x,5)+2*x+1.0/x;/*-*/ printf(f(%.2f) = %.2fn, x, y);20061 阶梯电价输入一个正整数repeat (0repeat10),做repeat次下列运算:为了提倡居民节约用电,某省电力公司执行阶梯电价,安装一户一表的居民用户电价分为两个阶梯:月用电量50千瓦时以内的,电价为0.53元/千瓦时;超过50千瓦时的用电量,电价上调0.05元/千瓦时。输入用户的月用电量e(千瓦时),计算并输出该用户应支付的电费(元),结果保留2位小数。输入输出示例:括号内是说明输入2 (repeat=2)10 (e=1
20、0)100 (e=100)输出cost = 5.30cost = 55.50#include int main(void) int repeat, ri; double cost, e; scanf(%d, &repeat); for(ri = 1; ri = repeat; ri+) scanf(%lf, &e); if(e=50) cost=e*0.53;elsecost=0.53*50+(e-50)*0.58;/*-*/ printf(cost = %.2fn, cost);20062 求m*m1/m(m+1)*(m+1)1/(m+1)(m+2)*(m+2)1/(m+2).n*n1/n输
21、入一个正整数repeat (0repeat10),做repeat次下列运算:输入两个正整数 m 和 n(m=n),求 sum = m*m1/m(m+1)*(m+1)1/(m+1)(m+2)*(m+2)1/(m+2).n*n1/n,结果保留6位小数。输入输出示例:括号内为说明输入3 (repeat=3)1 2 (m=1,n=2)2 5 (m=2,n=5)5 10 (m=5,n=10)输出sum = 6.sum = 55.sum = 355.#include int main(void) int i, m, n; int repeat, ri; double sum; scanf(%d,&repe
22、at); for(ri = 1; ri = repeat; ri+) scanf(%d%d, &m, &n); sum=0;for(i=m;i=n;i+)sum=sum+i*i+1.0/i;/*-*/ printf(sum = %.6fn, sum);20063 求12/33/54/75/9-6/11+输入一个正整数repeat (0repeat10),做repeat次下列运算:输入一个正整数 n,计算12/33/54/75/9-6/11+的前n项之和,输出时保留3位小数。输入输出示例:括号内是说明输入3 (repeat=3)1 (n=1)3 (n=3)5 (n=5)输出sum = 1.000
23、sum = 0.933sum = 0.917#include int main(void) int flag, i, n;float denominator; int repeat, ri; double item, sum; scanf(%d, &repeat); for(ri = 1; ri = repeat; ri+) scanf(%d, &n);sum=0;flag=1;denominator =1.0;for(i=1; i=n; i+)item=flag* i *1.0/ denominator;sum=sum+item;flag=-flag;denominator = denomi
24、nator +2;/*-*/ printf(sum = %.3fn, sum);20064 求2122232n#include #include int main(void) int i,n; int repeat, ri; double sum; scanf(%d, &repeat); for(ri = 1; ri = repeat; ri+) scanf(%d, &n);sum=0;for(i=1;i=n;i+)sum=sum+pow(2,i);/*-*/ printf(sum = %.0fn, sum);第4周(M4)10007 显示图案 (复习printf()的字符串输出) 编写程序,
25、在屏幕上显示如下图案。* * * * * * * * * *#include int main(void) printf(* * * *n * * *n * *n *n);/*-*/20042 生成阶乘表 输入一个正整数n,生成一张阶乘表,输出 1! n! 的值,要求定义和调用函数fact(n)计算 n!,函数类型为double。输出使用语句 printf(%d! = %.0fn, i, myfact);输入输出示例:括号内是说明输入3(n=3)输出1! = 12! = 23! = 6#include int main(void) int i, n; double myfact; double
26、 fact(int n); scanf(%d, &n); for(i=1;i=n;i+) myfact=fact(i); printf(%d! = %.0fn, i, myfact); double fact(int n) double result; int j; result=1; for(j=1;j=n;j+) result=result*j; return result; 20043 使用函数求 n! /(m!* (n-m)!) 输入一个正整数repeat (0repeat10),做repeat次下列运算:输入2个正整数 m 和 n(m=n),计算 n! /(m!* (n-m)!) 。
27、要求定义并调用函数fact(n)计算n的阶乘, 其中 n 的类型是 int,函数类型是 double。例:括号内是说明输入:2 (repeat=2)2 7 (m=2, n=7)5 12 (m=5, n=12)输出:result = 21result = 792#include stdio.hint main(void) int m, n; int repeat, ri; double s; double fact(int n); scanf(%d, &repeat); for(ri = 1; ri = repeat; ri+) scanf(%d%d, &m, &n); fact(n); fac
28、t(m); fact(n-m); s=1; s=s*fact(n)/(fact(m)*fact(n-m); printf(result = %.0fn, s); double fact(int n) double result; int i; result=1; for(i=1;i=n;i+) result=result*i; return result; 20054 求平均值 编写程序,输入 3 个整数,计算并输出它们的平均值(结果保留2位小数)。输入输出示例:输入1 2 3输出average = 2.00#include int main(void) int a, b, c; double
29、 average; scanf(%d%d%d,&a,&b,&c); average= (a+b+c )/3.0; printf(average = %.2fn, average); 20057 求11/21/3.1/n 输入一个正整数repeat (0repeat10),做repeat次下列运算:编写程序,输入一个正整数n,求11/21/3.的前n项之和,输出时保留6位小数。输入输出示例:括号内为说明输入2(repeat=2)6(计算1+1/2+1/3+1/4+1/5+1/6)2(计算1+1/2)输出sum = 2.sum = 1.#include int main(void) int i,
30、n; int repeat, ri; double sum; scanf(%d, &repeat); for(ri = 1; ri = repeat; ri+) scanf(%d, &n);sum=0;for(i=1;i=n;i+) sum=sum+1.0/i; printf(sum = %.6fn, sum); 20065 求0!1!2!n! 输入一个正整数repeat (0repeat10),做repeat次下列运算:编写一个程序,输入一个正整数 n,计算 e = 0!1!2!n!,要求定义和调用函数fact(n)计算n!,函数类型是double。例:括号内是说明输入3 (repeat=3
31、)124输出sum = 2sum = 4sum = 34#include int main(void) int i,n; int repeat, ri; double sum; double fact(int n); scanf(%d, &repeat); for(ri = 1; ri = repeat; ri+) scanf(%d, &n); sum=1; for(i=1;i=n;i+) fact(i); sum=sum+fact(i); /*-*/ printf(sum = %.0fn, sum); double fact(int n) double result; int j; resu
32、lt=1; for(j=1;j=n;j+) result=result*j; return result; 40015 求最小值 输入一个正整数repeat (0repeat10),做repeat次下列运算:输入一个正整数n, 再输入n个整数,输出最小值。输入输出示例:括号内是说明输入3 (repeat=3) 4 -2 -123 100 0 4 -9 -1 1 -8 3 5 3 1 输出min = -123min = -9min = 1#include int main(void) int i, min, n, x; int repeat, ri; scanf(%d, &repeat); fo
33、r(ri = 1; ri = repeat; ri+) scanf(%d, &n); scanf(%d, &x); min=x; for(i=1;ix) min=x; /*-*/ printf(min = %dn, min); 40018 求aaaaaaaaa提示: 第i项通项: tn = a + a * 10 + a * 100 +a * 10( n-1) = t(n-1) + a * 10(n-1)#include #include int main(void) int a, i, n, sn, tn; int ri, repeat; scanf(%d, &repeat); for(ri
34、= 1; ri = repeat; ri+) scanf(%ld%d, &a, &n); sn=0; tn=0; for (i=0; in; i+) tn=tn+a*pow(10,i); sn=sn+tn; /*-*/ printf(sum = %dn, sn); 第5周(M5)30001 求一元二次方程的根 输入一个正整数repeat (0repeat10),做repeat次下列运算:输入参数a,b,c,求一元二次方程a*x*xb*xc0的根,结果保留2位小数。输出使用以下语句:printf(参数都为零,方程无意义!n);printf(a和b为0,c不为0,方程不成立n);printf(x
35、= %0.2fn, -c/b);printf(x1 = %0.2fn, (-b+sqrt(d)/(2*a);printf(x2 = %0.2fn, (-b-sqrt(d)/(2*a);printf(x1 = %0.2f+%0.2fin, -b/(2*a), sqrt(-d)/(2*a);printf(x2 = %0.2f-%0.2fin, -b/(2*a), sqrt(-d)/(2*a);输入输出示例:括号内为说明输入:5 (repeat=5)0 0 0 (a=0,b=0,c=0)0 0 1 (a=0,b=0,c=1)0 2 4 (a=0,b=2,c=4)2.1 8.9 3.5 (a=2.1,b=8.9,c=3.5)1 2 3 (a=1,b=2,c=3)输出:参数都为零,方程无意义!a和b为0,c不为0,方程不成立x = -2.00x1 = -0.44x2