浙江大学C程题库(更新).docx
精品文档,仅供学习与交流,如有侵权请联系网站删除c语言程序设计题目及答案 20021程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入 x,计算并输出下列分段函数 f(x) 的值(保留1位小数)。当 x 不等于0时,y = f(x) = 1/x,当 x 等于0时,y = f(x) = 0。输入输出示例:括号内是说明输入2 (repeat=2)10 (x=10)0 (x=0)输出f(10.00) = 0.1f(0.00) = 0.0#include <stdio.h>int main(void) int repeat, ri; double x, y; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri+) scanf("%lf", &x); if(x!=0) y=1/x;else y=0; printf("f(%.2f) = %.1fn", x, y); 20022程序填空,不要改变与输入输出有关的语句。输入华氏温度,输出对应的摄氏温度。计算公式:c = 5*(f-32)/9,式中:c表示摄氏温度,f表示华氏温度。输入输出示例:括号内为说明输入150 (fahr=150)输出celsius = 65#include <stdio.h>int main(void)int celsius, fahr; scanf(“%d”,&fahr); celsius=5.0*(fahr-32)/9; printf("celsius = %dn", celsius); 20023程序填空,不要改变与输入输出有关的语句。输入存款金额 money、存期 year 和年利率 rate,根据下列公式计算存款到期时的利息 interest(税前),输出时保留2位小数。interest = money(1+rate)year - money输入输出示例:括号内为说明输入1000 3 0.025 (money = 1000, year = 3, rate = 0.025)输出interest = 76.89#include <stdio.h>#include <math.h>int main(void) int money, year;double interest, rate;scanf(“%d%d%lf”,&money,&year,&rate); interest=money*pow(1+rate),year)-money; printf("interest = %.2fn", interest); 20024程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入 x,计算并输出下列分段函数 f(x) 的值(保留2位小数),请调用 sqrt 函数求平方根,调用 pow 函数求幂。当x >= 0时,f(x) = x0.5,当x小于0时,f(x) = (x+1)2 + 2x + 1/x。输入输出示例:括号内是说明输入3 (repeat=3)10-0.50输出f(10.00) = 3.16f(-0.50) = -2.75f(0.00) = 0.00#include <stdio.h>#include <math.h>int main(void) int repeat, ri; double x, y; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri+)scanf("%lf", &x);if(x>=0) y=sqrt(x);else y=pow(x+1),2)+2*x+1/x; printf("f(%.2f) = %.2fn", x, y); 20025程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入实数 x,计算并输出下列分段函数 f(x) 的值,输出时保留1位小数。当 x 不等于10时,y = f(x) = x,当 x 等于10时,y = f(x) = 1/x。输入输出示例:括号内是说明输入2 (repeat=2)10234输出f(10.0) = 0.1f(234.0) = 234.0#include <stdio.h>int main(void) int repeat, ri; double x, y; scanf("%d", &repeat);for(ri = 1; ri <= repeat; ri+)scanf("%lf", &x);if(x!=10) y=x;else y=1/x; printf("f(%.1f) = %.1fn", x, y); 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 <stdio.h>int main(void)int num1, 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;20031程序填空,不要改变与输入输出有关的语句。计算表达式 1 + 2 + 3 + . + 100的值。输出示例:sum = 5050#include <stdio.h>int main(void)int i, sum;sum=0;for(i=1;i<=100;i+) sum=sum+i; printf("sum = %dn", sum); 20032程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数m(0<=m<=100),计算表达式 m + (m+1) + (m+2) + . + 100的值。输入输出示例:括号内为说明输入3 (repeat=3)0 (计算0+1+2+.+100)10 (计算10+11+12+.+100)50 (计算50+51+52+.+100)输出sum = 5050sum = 5005sum = 3825#include <stdio.h>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程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做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 <stdio.h>int main(void) int i, m, n; int repeat, ri; double sum; scanf("%d", &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程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数 n,计算表达式 1 + 1/3 + 1/5 + . 的前 n 项之和,输出时保留6位小数。输入输出示例:括号内为说明输入2 (repeat=2)5 (计算1+1/3+1/5+1/7+1/9)23 (计算1+1/3+1/5+.+1/45)输出sum = 1.787302sum = 2.549541#include <stdio.h>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+1.0/(2*i-1); printf("sum = %.6fn", sum);20035程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做repeat次下列运算:读入一个正整数 n,计算11/41/71/10的前 n 项之和,输出时保留3位小数。输入输出示例:括号内是说明输入2 (repeat=2)310输出sum = 0.893sum = 0.819#include <stdio.h>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=1;item=0;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程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做repeat次下列运算:读入2个整数 lower 和 upper,输出一张华氏摄氏温度转换表,华氏温度的取值范围是lower, upper,每次增加2F。计算公式:c = 5 * (f - 32) / 9,其中:c表示摄氏温度,f表示华氏温度。输出请使用语句 printf("%3.0f %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 <stdio.h>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); printf("fahr celsiusn");for(fahr=lower;fahr<=upper;fahr=fahr+2) celsius=5 * (fahr- 32) / 9; printf("%3.0f %6.1fn", fahr, celsius);20037程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入2 个正整数 m 和 n,计算 m!n!。输入输出示例:括号内是说明输入:2 (repeat=2)1 4 (m=1,n=4)3 8 (m=3,n=8)输出:1! + 4! = 253! + 8! = 40326#include "stdio.h"int main(void) int i, m, n; int repeat, ri; double fm, fn; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri+) scanf("%d%d", &m, &n); fm=fn=1; for(i=1;i<=m;i+) fm=fm*i; for(i=1;i<=n;i+) fn=fn*i; printf("%d! + %d! = %.0fn", m, n, fm+fn); 20038程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做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.00#include <stdio.h>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程序填空,不要改变与输入输出有关的语句。输入一个正整数n,生成一张3的乘方表,输出30 3n的值,可调用幂函数计算3的乘方。输出使用语句 printf("pow(3,%d) = %.0fn", i, mypow);输入输出示例:括号内是说明输入3 (n=3)输出pow(3,0) = 1pow(3,1) = 3pow(3,2) = 9pow(3,3) = 27#include <stdio.h>#include <math.h>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); return 0;20042程序填空,不要改变与输入输出有关的语句。输入一个正整数n,生成一张阶乘表,输出 1! n! 的值,要求定义和调用函数fact(n)计算 n!,函数类型为double。输出使用语句 printf("%d! = %.0fn", i, myfact);输入输出示例:括号内是说明输入3 (n=3)输出1! = 12! = 23! = 6#include <stdio.h>int main(void) int i, n; double myfact; double fact(int n);scanf("%d", &n);for(i=1;i<=n;i+) myfact=fact(i); printf("%d! = %.0fn", i, myfact); return 0;double fact(int n)int i;double f=1;for(i=1;i<=n;i+)f=f*i;return f;20043程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入2个正整数 m 和 n(m<=n),计算 n! /(m!* (n-m)!) 。要求定义并调用函数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.h"double fact(int n);int main(void) int m, n; int repeat, ri; double s; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri+) scanf("%d%d", &m, &n); s= fact(n)/(fact(m)*fact(n-m); printf("result = %.0fn", s);return 0;double fact(int n)int i;double f=1;for(i=1;i<=n;i+)f=f*i;return f;20044程序填空,不要改变与输入输出有关的语句。计算 1000.51010.510000.5的值(保留2位小数),可调用sqrt函数计算平方根。输入输出示例:括号内是说明输出sum = 20435.99#include <stdio.h>#include <math.h>int main(void) int i; double sum; sum=0; for(i=100;i<=1000;i+) sum=sum+sqrt(i); printf("sum = %.2fn", sum);30001程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入参数a,b,c,求一元二次方程a*x*xb*xc0的根,结果保留2位小数。输出使用以下语句:printf("参数都为零,方程无意义!n");printf("a和b为0,c不为0,方程不成立n");printf("x = %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 = -3.80x1 = -1.00+1.41ix2 = -1.00-1.41i#include <stdio.h>#include <math.h>int main(void) int repeat, ri; double a, b, c, d; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri+) scanf("%lf%lf%lf", &a, &b, &c); d=b*b-4*a*c;if(a=0)if(b=0)if(c=0) printf("参数都为零,方程无意义!n"); else printf("a和b为0,c不为0,方程不成立n"); else printf("x = %0.2fn", -c/b); else if(d>=0)printf("x1 = %0.2fn", (-b+sqrt(d)/(2*a);printf("x2 = %0.2fn", (-b-sqrt(d)/(2*a); else 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);30002程序填空,不要改变与输入输出有关的语句。输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:输入一个整数 x,计算并输出下列分段函数 sign(x) 的值。 -1 x < 0y = sign(x) = 0 x = 0 1 x > 0输入输出示例:括号内是说明输入3 (repeat=3) 10 (x=10) 0 (x=0) -98 (x=-98) 输出sign(10) = 1 (x = 10时 y = 1)sign(0) = 0 (x = 0时 y = 0)sign(-98) = -1 (x = -98时y = -1)#include <stdio.h>int main(void) int repeat, ri; int x, y; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri+) scanf("%d", &x); if(x>0) y=1;else if(x=0) y=0;else y=-1; printf("sign(%d) = %dn", x, y);return 0;30003程序填空,不要改变与输入输出有关的语句。输入10个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。输入输出示例:括号内是说明输入Reold 123?输出letter = 5, blank = 1, digit = 3, other = 1#include <stdio.h>int main(void) char c; int blank, digit, i, letter, other; blank = digit = letter = other = 0; for(i = 1; i <= 10; i+) c = getchar(); if(c >= 'a' && c <= 'z' ) | ( c >= 'A' && c <= 'Z') letter +; else if(c >='0'&&c<='9') digit +; else if(c = ' ' | c = 'n') blank +; else other +;printf("letter = %d, blank = %d, digit = %d, other = %dn", letter, blank, digit, other);return 0;30004程序填空,不要改变与输入输出有关的语句。输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:输入五级制成绩(AE),输出相应的百分制成绩(0100)区间,要求使用switch语句。五级制成绩对应的百分制成绩区间为:A(90-100)、B(80-89)、C(70-79)、D(60-69)和E(0-59),如果输入不正确的成绩,显示"Invalid input"。输出使用以下语句:printf("90-100n"); printf("80-89n"); printf("70-79n"); printf("60-69n");printf("0-59n");printf("Invalid inputn");输入输出示例:括号内是说明输入6ABCDEj (repeat=6,输入的五级成绩分别为A、B、C、D、E和无效的字符j) 输出90-10080-8970-7960-690-59Invalid input (输入数据不合法)#include <stdio.h>int main(void) char ch; int repeat, ri; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri+) ch = getchar(); switch(ch) case 'A: printf("90-100n");break; case 'B': printf("80-89n");break; case 'C' : printf("70-79n");break;case 'D' : printf("60-69n");break;case 'E' : printf("0-59n"); break; default: printf("Invalid inputn"); break;return 0;30005程序填空,不要改变与输入输出有关的语句。查询水果的单价。有4种水果,苹果(apples)、梨(pears)、桔子(oranges)和葡萄(grapes),单价分别是3.00元/公斤,2.50元/公斤,4.10元/公斤和10.20元/公斤。在屏幕上显示以下菜单(编号和选项),用户可以连续查询水果的单价,当查询次数超过5次时,自动退出查询;不到5次时,用户可以选择退出。当用户输入编号14,显示相应水果的单价(保留1位小数);输入0,退出查询;输入其他编号,显示价格为0。输入输出示例:括号内是说明输入3 (oranges的编号) 0 (退出查询) 输出1 apples2 pears3 oranges4 grapes0 Exitprice = 4.11 apples2 pears3 oranges4 grapes0 Exit#include <stdio.h>int main(void) int choice, i; double price; for(i = 1; i <= 5; i+) printf("1 applesn"); printf("2 pearsn"); printf("3 orangesn"); printf("4 grapesn"); printf("0 Exitn"); scanf("%d", &choice); if(choice = 0) break; else switch (choice) case 1: price=3.0; break; case 2: price=2.5; break; case 3: price=4.1; break; case 4: price=10.2; break; default: price=0.0; break; printf("price = %0.1fn", price); return 0;30006程序填空,不要改变与输入输出有关的语句。输入5个学生的数学成绩,判断他们的成绩是否及格。如果成绩低于60,输出"Fail",否则,输出"Pass"。输入输出示例:括号内是说明输入6159924060输出PassFailPassFailPass#include <stdio.h>int main(void)0 int i, mark; for(i = 1; i <= 5; i+) scanf("%d", &mark); if(mark>=60) printf(“Passn”); else printf(“Failn”);30007程序填空,不要改变与输入输出有关的语句。输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:输入三角形的3条边 a, b, c,如果能构成一个三角形,输出面积 area 和周长 perimeter(保留2位小数);否则,输出"These sides do not correspond to a valid triangle"。在一个三角形中,任意两边之和大于第三边。三角形面积计算公式:area = (s(s-a)(s-b)(s-c)0.5 ,其中s = (a+b+c)/2输出使用以下语句:printf("area = %.2f, perimeter = %.2fn",area, perimeter);printf("These sides do not correspond to a valid trianglen");输入输出示例:括号内是说明输入2 (repeat=2) 5 5 3 (a=5,b=5,c=3)1 4 1 (a=1,b=4,c=1)输出area = 7.15, perimeter = 13.00These sides do not correspond to a valid triangle#include <stdio.h>#include <math.h>int main(void) int a, b, c; int repeat, ri; double area, perimeter, s; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri+) scan