C语言题目及解答 - C语言 题目及解答 1000: A+B Problem 样例输入 1 2 样例输出 3 #include <stdio.h》 void main { int a,b,sum; printf(“”); scanf(“%d%d”,-a,-b); sum=a+b; printf(“%d\\n”,sum); } 1003: 2022年春浙江省计算机等级考试二级C 编程题(1) 输入x ,计算并输出以下分段函数 f(x) 的值可以调用数学库函数:平方根函数sqrt,绝对值函数fabs 和幂函数 pow 保存2位小数 输入 x 输出 f(x) 样例输入 5 样例输出 15.00 #include “stdio.h” #include “math.h” void main { double x,y; scanf(“%lf”,-x); if(x<0) { y=fabs(x); } if(x<2--x》=0) { y=sqrt(x+1); } if(x》=2--x<4) { y=(x+2)*(x+2)*(x+2)*(x+2)*(x+2); } if(x》=4) { y=2*x+5; } printf(“%.2f”,y); } 1007: 2022年春浙江省计算机等级考试二级C 编程题(2) 编写程序,输入一个正整数n,求以下算式的值。
要求定义和调用函数fact(k)计算k的阶乘,函数返回值的类型是double 1+1/2+ .... +1/n! 输出保存5位小数 输入 输出 样例输入 5 样例输出 sum=1.71667 #include “stdio.h” void main { long i,n; float s=0,t=1; scanf(“%ld”,-n); for(i=1;i<=n;i++) { t=t*i; s=s+1/t; } printf(“sum=%.5f”,s); } 3: 2022年秋浙江省计算机等级考试二级C 编程题(2) 编制函数,其功能是在float类型一维数组中查找最大值、最小值,并将它们返回到调用程序 * 输出保存两位小数 输入 n n个浮点数 输出 最大值 最小值 样例输入 10 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 样例输出 10.00 1.00 #include “stdio.h” void main { int i,n; float min,max,a[100]; scanf(“%d”,-n); for(i=0;i<n;i++) { scanf(“%f”,-a[i]); } max=a[0];min=a[0]; for(i=1;i<n;i++) {if(a[i]<min)min=a[i]; if(a[i]》max)max=a[i];} printf(“%.2f %.2f”,max,min); } 4: 2022年秋浙江省计算机等级考试二级C 编程题(1) 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
输入 一行字符 输出 统计值 样例输入 aklsjflj123 sadf918u324 asdf91u32oasdf/.';123 样例输出 23 16 2 4 /* Note:Your choice is C IDE */ #include “stdio.h” void main { char c; int l=0,s=0,n=0,r=0; while((c=getchar)!='\\n') if(c<='z'--c》='a'||c<='Z'--c》='A') l++; else if(c==' ') s++; else if(c<='9'--c》='0') n++; else r++; printf(“%d %d %d %d”,l,n,s,r); } 5: 2022年秋浙江省计算机等级考试二级C 编程题(2) 输入一个正整数n.求1+1/2!+1/3!+....+1/n! 要求定义并调用函数fact(n)计算n的阶乘,函数返回值的类型是点单精度浮点型 * 输出保存4位小数 输入 正整数n 输出 数列之和 样例输入 2 样例输出 1.5000 #include “stdio.h” void main { long i,n; float s=0,t=1; scanf(“%ld”,-n); for(i=1;i<=n;i++) { t=t*i; s=s+1/t; } printf(“%.4f”,s); } 6: 2022年秋浙江省计算机等级考试二级C 编程题(1) 输入10个数,求它们的平均值,并输出大于平均值的数据的个数。
输入 10个数 输出 大于平均数的个数 样例输入 1 2 3 4 5 6 7 8 9 10 样例输出 5 /* Note:Your choice is C IDE */ #include “stdio.h” void main { int a[10],i,n=0,s=0,b=0; for(i=0;i<10;i++) {scanf(“%d”,-a[i]); s=a[i]+s; } b=s/10; for(i=0;i<10;i++) { if(a[i]》b) 第 5 页 共 5 页。