文档详情

C语言练习题标准答案.docx

发布:2019-09-27约8.62万字共186页下载文档
文本预览下载声明
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 = %.2f\n 程序运行示例如下: Input a,b,c:3,4,5 area = 6.00 答案:#includestdio.h #includemath.h int main(void) { float a, b, c, s, area; printf(Input a,b,c:); scanf(%f,%f,%f, a, b, c); s = (a + b + c) / 2; area = sqrt(s*(s - a)*(s - b)*(s - c)); printf(area = %.2f\n, area); return 0; } -----------------1 2.从键盘任意输入一个4位整数,编程计算并输出它的逆序数.例如:输入1234,分离出千位1、百位2、十位3和个位4,然后计算4*1000+3*100+2*10+1=4321,并输出4321。 **要求输入提示信息为:无 **要求输入格式为: %d **要求输出格式为:%d 程序运行示例如下: 1234 此处为输入 4321 此处为输出 答案: #includestdio.h int main(void) { int a, b, c, d,e; scanf(%d,a); 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; } --------------2 3.有关输入输出问题。 输入为:12a↙ b↙ 运行结果为:1,2,a,b,123.300000,65535 请改正程序中的错误,使它能得出正确的结果。 #include stdio.h main() { int b; unsigned short a = 65535; short k = a; char c, d; int f, g; b = (1234.0 - 1) / 10; scanf(%c, c); scanf(%c, d); scanf(%d, f); scanf(%d, g); printf(%c,%c,%c,%c,%f,%d, c, d , f, g, b, k); } 答案:#include stdio.h main() { double b; int a = 65535; char c, d; int f, g; b = (1234.0 - 1) / 10; scanf(%c, c); scanf(%c, d); scanf(%c\t, f); scanf(%c, g); printf(%c,%c,%c,%c,%f,%d, c, d , f, g, b, a); } --------3 4.输入一行字符,统计其中的英文字符、数字字符、空格字符,以及其他字符的个数。请找出以下程序的错误,并改正之。 #include stdio.h #include string.h ? #define ARR_SIZE = 80;? ? main() { ????char str[ARR_SIZE]; ????int? len, i; ????int? letter=0,digit=0,space=0,other=0; ? ????printf(请输入一个字符串:); ????gets(str); ? ????len = strlen(str);??? ? ????for (i=0; ilen; i++) ????{ ????????if (a=str[i]=z || A=str[i]=Z) ????????{ ????????????letter ++;?? ????????}??????????????? ????????else if (0=str[i]=9) ????????{ ????????????digit ++;????? ????????}?????????
显示全部
相似文档