C语言学习总结分析题'答案.doc
《C语言学习总结分析题'答案.doc》由会员分享,可在线阅读,更多相关《C语言学习总结分析题'答案.doc(106页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、1.已知三角形的三边长为 a,b,c,计算三角形面积的公式为:area=sqrt(s(s-a)(s-b)(s-c),其中 s=(a+b+c)/2。试编程从键盘输入 a,b,c 的值(假设 a,b,c 的值可以保证其构成一个三角形),计算并输出三角形的面积。*输入格式要求:“%f,%f,%f“ 提示信息:“Input a,b,c:“*输出格式要求:“area = %.2fn“程序运行示例如下:Input a,b,c:3,4,5area = 6.00答案:#include#includeint main(void)float a, b, c, s, area;printf(“Input a,b,c
2、:“);scanf(“%f,%f,%f“, s = (a + b + c) / 2;area = sqrt(s*(s - a)*(s - b)*(s - c);printf(“area = %.2fn“, area);return 0;-12.从键盘任意输入一个 4 位整数,编程计算并输出它的逆序数.例如:输入 1234,分离出千位 1、百位 2、十位 3 和个位 4,然后计算 4*1000+3*100+2*10+1=4321,并输出 4321。*要求输入提示信息为:无*要求输入格式为: “%d“*要求输出格式为:“%d“程序运行示例如下:1234 此处为输入4321 此处为输出答案:#inc
3、ludeint main(void)int a, b, c, d,e;scanf(“%d“,b = a/1000;c = a%1000/100;d = a%100/10;e = a%10;a = e*1000+d*100+c*10+b;printf(“%d“,a);return 0;-23.有关输入输出问题。输入为:12ab 运行结果为:1,2,a,b,123.300000,65535请改正程序中的错误,使它能得出正确的结果。#include main()int b;unsigned short a = 65535;short k = a;char c, d;int f, g;b = (123
4、4.0 - 1) / 10;scanf(“%c“, scanf(“%c“, scanf(“%d“, scanf(“%d“, printf(“%c,%c,%c,%c,%f,%d“, c, d , f, g, b, k);答案:#include main()double b;int a = 65535;char c, d;int f, g;b = (1234.0 - 1) / 10;scanf(“%c“, scanf(“%c“, scanf(“%ct“, scanf(“%c“, printf(“%c,%c,%c,%c,%f,%d“, c, d , f, g, b, a);-34.输入一行字符,统计
5、其中的英文字符、数字字符、空格字符,以及其他字符的个数。请找出以下程序的错误,并改正之。#include #include #define ARR_SIZE = 80; main()char strARR_SIZE;int len, i;int letter=0,digit=0,space=0,other=0;printf(“请输入一个字符串:“);gets(str);len = strlen(str); for (i=0; i#include #define ARR_SIZE 80 main()char strARR_SIZE;int len, i;int letter = 0, digit
6、 = 0, space = 0, other = 0;printf(“请输入一个字符串:“);gets(str);len = strlen(str);for (i = 0; iint Fun(int a)int i;if(a1)for(i = 2;i#define N 330int aN;int main()int c = 0, b = 0, d = 1;int i = 0, j = 0, k = 0;for (i = 3;i = 0;c-)for (b = c + 1;c #include int IsPrime(int x);void OutputPrimeFactor(int x);in
7、t main()int m;printf(“Input m:“);scanf(“%d“,if(IsPrime(m)printf(“It is a prime numbern“);elseprintf(“%d = “,m);OutputPrimeFactor(m);return 0;int IsPrime(int x)int i,flag=1;int squareRoot=sqrt(x);if(x#include int main()int a;char b32;printf(“Input number:“);scanf(“%d“, printf(“number of decimal form:
8、%dn“, a);printf(“ its binary form: “);printf(“%sn“, itoa(a, b, 2);-89.求自然数 n 最近的素数 k, kn。*输入提示信息*:“Input n:“*输入数据格式*:“%d“*输出数据格式*:“%d“示例输入:257输出:Please input n:263答案:#includeint main()int n, i = 1, k;printf(“Please input n:“);scanf(“%d“, for (k = n + 1;i;k+)for (i = 2;i int IsPerfect(int x);int main
9、()int m;printf(“Input m:n“);scanf(“%d“, if (IsPerfect(m)printf(“%d is a perfect numbern“, m);elseprintf(“%d is not a perfect numbern“, m);int IsPerfect(int x)int i = 1,b = 0;for (i = 1;i int max(int a, int b)if(a = b)return a;elsereturn b;main()int a = 0,b = 0;printf(“Input a,b:“);scanf(“%d,%d“,prin
10、tf(“max = %dn“,max(a,b);-1112.利用求阶乘函数 Fact(),编程计算并输出 1! + 2! + + n!的值。*输入格式要求:“%u“ 提示信息:“Input n(n0):“*输出格式要求:“sum = %lun“答案:#include int sum_s(int i);main()unsigned int n = 0;unsigned long b = 0;printf(“Input n(n0):“);scanf(“%u“,for (b;n;n-)b = b +sum_s(n);printf(“sum = %lun“,b); int sum_s(int i)in
11、t a;for(a = 1;i;i-)if (i 1)a = a * i;elsea = a * 1;return a;-1213.编程输出如下菜单界面(具体的输出则以下面提示中要求的输出格式为准):=大学信息管理系统= -办公室管理 财务管理 教务管理 图书管理 科研管理 设备管理 人事管理 后勤管理 退出系统-请您在上述功能中选择(08):提示:输出如下格式“=大学信息管理系统= n“-n“1. 办公室管理 5. 财务管理 n“2. 教务管理 6. 图书管理 n“3. 科研管理 7. 设备管理 n“4. 人事管理 8. 后勤管理 n“0. 退出系统n“-n“请您在上述功能中选择(08):n
12、“答案:#include main()printf(“=大学信息管理系统= n“);printf(“-n“);printf(“1. 办公室管理 5. 财务管理 n“);printf(“2. 教务管理 6. 图书管理 n“);printf(“3. 科研管理 7. 设备管理 n“);printf(“4. 人事管理 8. 后勤管理 n“);printf(“0. 退出系统n“);printf(“-n“);printf(“请您在上述功能中选择(08):n“);-1314.利用静态变量计算 n 的阶乘。*输入格式要求:“%d“ 提示信息:“Input n:“*输出格式要求:“%d! = %ldn“程序运行
13、示例如下:Input n:101! = 12! = 23! = 64! = 245! = 1206! = 7207! = 50408! = 403209! = 36288010! = 3628800答案:#include int sum_s(int i);main()unsigned int n = 0,b = 1;printf(“Input n:“);scanf(“%d“,for (b = 1;b 1)a = a * i;elsea = a * 1;return a;-1415.写一个程序从键盘输入两个整数,然后显示输出第 1 个数除以第 2 个数的结果。例如,输入 123 和12,程序的输
14、出结果格式如下:10 Remainder = 3-12 ) 123提示:使用求余运算符获得余数,使用整数除法获得商。*输入格式要求:“%d%d“ 提示信息:“请输入两个整数:“*输出格式要求:“%13d Remainder = %dn“ -n“%5d ) %5d“答案:#include main()int a, b, c, d;printf(“请输入两个整数:“);scanf(“%d%d“,c = a / b;d = a % b;printf(“%13d Remainder = %dn“,c,d);printf(“ -n“);printf(“%5d ) %5d“,b,a);-1516.写一个程
15、序,将接收的摄氏温度转换为对应的华氏温度。程序应显示如下的提示信息:Please input cels: 然后输入一个十进制数并回车,然后程序以合适的消息形式输出转换后的华氏温度。程序使用如下的公式完成转换:摄氏温度 = 5.0 *(华氏温度 32.0) / 9.0*输入格式要求:“%lf“ 提示信息:“Please input cels: “*输出格式要求:“The fahr is: %.2f“答案:#include main()double a,b;printf(“Please input cels: “);scanf(“%lf“,b = (9.0 * a)/5.0 + 32.0;prin
16、tf(“The fahr is: %.2f“,b);-1617.写一个程序,将接收的华氏温度转换为对应的摄氏温度。程序应显示如下的提示信息:Please input fahr: 然后输入一个十进制数并回车,然后程序以合适的消息形式输出转换后的华氏温度。程序使用如下的公式完成转换:摄氏温度 = 5.0 *(华氏温度 32.0) / 9.0*输入格式要求:“%lf“ 提示信息:“Please input fahr: “*输出格式要求:“The cels is: %.2f“答案:#include main()double a,b;printf(“Please input fahr: “);scanf
17、(“%lf“,b = 5.0 * (a - 32.0) / 9.0;printf(“The cels is: %.2f“,b);-1718.编写一个程序,输入一个 3 位整数,要求逆序输出对应的数。例如:输入 123,则输出 321。*输入格式要求:“%d“ 提示信息:“Please enter a positive integer: “*输出格式要求:“%d%dn“ 程序运行示例如下:Please enter a positive integer: 345345543答案:#includeint main(void)int a, b, c, d,e;printf(“Please enter
18、a positive integer: “);scanf(“%d“,b = a/100;d = a%100/10;e = a%10;c = e*100+d*10+b;printf(“%d%d“,a,c);return 0;-1819.计算两个数的平方和从键盘读入两个实数,编程计算并输出它们的平方和,要求使用数学函数 pow(x,y)计算平方值,输出结果保留 2 位小数。提示:使用数学函数需要在程序中加入编译预处理命令 #include 以下为程序的输出示例:Please input x and y:1.2,3.4result=13.00输入格式:“%f,%f“输出格式:输入提示信息:“Plea
19、se input x and y:n“输出格式:“result=%.2fn“答案:#include #include main()float x,y,z;printf(“Please input x and y:n“);scanf(“%f,%f“,z = pow(x,2) + pow(y,2);printf(“result=%.2fn“,z);1920.写一个程序从键盘输入两个整数,然后显示第 1 个整数占第二个整数的百分比,保留到小数点后 1位。例如输入 5 和 40,程序输出结果如下:5 is 12.5 percent of 40 *输入格式要求:“%d%d“ 提示信息:“请输入两个整数:
20、“*输出格式要求:“%d is %.1f percent of %d“答案:#include main()int a, b;float c;printf(“请输入两个整数:“);scanf(“%d%d“,c = (float)a / (float)b;c = c *100;printf(“%d is %.1f percent of %d“,a, c, b );-2021.求三角形面积。输入三角形的三个边长,计算三角形的面积。设三角形的三个边长分别为a、b、c,为简单起见,我们认为输入的三个边长数据是正确的,可以组成一个三角形。*输入格式要求:“%f,%f,%f“ 提示信息:“Enter 3 f
21、loats:“*输出格式要求:“area=%.2fn“程序运行示例如下:Enter 3 floats:2,2,3area=1.98答案:#include#includeint main(void)float a, b, c, s, area;printf(“Enter 3 floats:“);scanf(“%f,%f,%f“, s = (a + b + c) / 2;area = sqrt(s*(s - a)*(s - b)*(s - c);printf(“area=%.2fn“, area);return 0;-2122.写一个程序计算长为 11.5 厘米,宽为 2.5 厘米,高为 10 厘
22、米的盒子的体积和表面积。程序说明:采用直接赋值,不用输入。*输出格式要求“area=%.2f,volume=%.2f“答案:#include main()float a, b;a = (11.5 * 2.5 + 11.5*10 +10 * 2.5)*2;b = 11.5 * 2.5 * 10;printf(“area=%.2f,volume=%.2f“,a,b);23.用递归方法计算整数 n 的阶乘 n!。*输入格式要求:“%d“ 提示信息:“Input n:“ “nint f(int n)if(n=1 |n=0)return 1;elsereturn n * f(n - 1);main()i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 学习 总结 分析 39 答案
限制150内