admin 管理员组文章数量: 1086019
2024年4月22日发(作者:编程random是什么意思)
数学11—1 C语言平时训练题
1、算术基本运算
Description
计算两整数x和y(0〈x,y<1000)的和、差、积、商、余数、x的平方和y的三次方。
Input
输入只有一行.
Output
输出为多行,按顺序每行输出x,y的和、差、积、商、余数、x的平方和y的三次方。
Sample Input
x = 11, y = 3
Sample Output
x + y : 14
x - y : 8
x * y : 33
x / y quotient: 3, remainder: 2
x ^ 2 : 121
y ^ 3 : 27
Answer
#include 〈stdio.h>
int main()
{
int x,y,a,b,c,d,e,f,g;
0 scanf(”x = %d, y = %d”,&x,&y); a=x+y; b=x-y; c=x*y; d=x/y; e=x%y; f=x*x; g=y*y*y; printf(”x + y : %dn”,a); printf("x - y : %dn",b); printf("x * y : %dn”,c); printf("x / y quotient: %d, remainder: %dn”,d,e); printf(”x ^ 2 : %dn”,f); printf(”y ^ 3 : %dn”,g); return 0; } 2、求圆的面积和周长 Description 从键盘输入圆的半径,求圆的面积和周长,圆周率取3.14。 Input 输入一个浮点型数据,有效数字不会超过十进制的6位。 Output 输出为两行。 第一行为圆的面积,第二行为圆的周长,格式见sample。 Sample Input 3 Sample Output Area: 28。260000 Perimeter: 18.840000 Answer #include〈stdio。h〉 #define PI 3.14 int main() { float r,s,c; scanf("%f”,&r); s=PI*r*r; c=2*PI*r; printf("Area: %fn",s); printf(”Perimeter: %fn”,c); return 0; } 3、 平均值 Description 求3个数的平均值。 Input 输入只有一行,为3个较小的整数. Output 输出为这3个整数的平均值,保留3位小数。 Sample Input 1 2 3 Sample Output 2。000 Answer #include 〈stdio.h> int main() { int a,b,c; float d; scanf(”%d %d %d",&a,&b,&c); d=(a+b+c)/3。0; printf("%.3fn",d); return 0; } 4、货币兑换 Description 给出人民币对美元、欧元、日元的当日汇率,求给定金额的人民币能兑换成外币的金额,求给定金额的外币 能兑换成人民币的金额。 要计算的外币有三种:美元、欧元、日元。 Input 输入有三行。 第一行依次为美元、欧元、日元外币汇率,用空格分开.汇率用100外币为单位,精确到小数点后4位,如 668.5200表示“100美元=668.5200人民币”。汇率浮动范围为(0,10000). 第二行为外币金额x,第三行为人民币金额y。x,y均为整数,且0〈x,y<10000。 Output 输出为两行。 第一行为金额为x的美元、欧元、日元兑换成人民币的金额,用空格分开。 第二行为金额为y的人民币兑换成美元、欧元、日元的金额,用空格分开。 所有金额精确到小数点后两位。 Sample Input 668。5200 908。0685 7。9852 1500 1500 Sample Output 10027.80 13621.03 119。78 224。38 165。19 18784.75 Answer #include int main() { double x,y,a,b,c,d,e,f,g,h,i; scanf("%lf%lf%lf”,&a,&b,&c); scanf(”%lf”,&x); scanf(”%lf”,&y); d=x/100*a; e=x/100*b; f=x/100*c; g=y/a*100; h=y/b*100; i=y/c*100; printf("%.2lf %.2lf %。2lfn",d,e,f); printf("%。2lf %。2lf %。2lfn",g,h,i); return 0; } 5、 求字符的值 Description 从键盘输入3个字符(不含双字节字符),分别输出每个字符的十进制值(ASCII码)、八进制值和十六进制 值。 Input 输入为3个字符。 Output 输出为3行。 每一行为每个字符(对应输入顺序)的十进制、八进制和十六进制值,用空格分隔开.每个输出的值占3个 字符,不足3个字符前面补0。 Sample Input 0 A Sample Output 048 060 030 032 040 020 065 101 041 Answer #include int main() { char a,b,c; scanf(”%c%c%c",&a,&b,&c); printf(”%。3d %。3o %。3xn",a,a,a); printf("%。3d %。3o %.3xn”,b,b,b); printf("%。3d %。3o %.3xn",c,c,c); return 0; } 6、 奇数还是偶数? Description 输入一个整数,判读它是奇数还是偶数. Input 输入只有一行,为一个100以内的正整数。 Output 输出为一行。 若输入为偶数则输出“even",奇数输出“odd”。 Sample Input 30 Sample Output even Answer #include int main() { int a; scanf(”%d”,&a); if(a>=0&&a<=100) { if (a%2==0) printf(”evenn”); else printf("oddn"); } else printf(”error”); return 0; } 7、绝对值 Description 求整型数据和浮点型数据的绝对值。 Input 输入两个数,第一个是整数,第二个是浮点数。 Output 输出为两行,第一行为整数的绝对值,第二行为浮点数的绝对值,注意浮点数的绝对值不输出无意义的0。 Sample Input -1 1 Sample Output 1 1 Answer #include #include #include〈stdlib。h〉 int main() { int a,c; double b,d; scanf(”%dn%lf",&a,&b); c=abs(a); d=fabs(b); printf("%dn%g",c,d); return 0; } 8、简单的打折计算 Description 商店规定:消费满n元,可以打八八折。设某件商品标价m元,输入购买的件数x,计算出需要支付的金额(单 位:元),精确到分。 Input 输入只有一行,三个整数m、n和x,且0〈x〈m〈n<1000。 Output 输出金额,精确到分。 Sample Input 95 300 4 Sample Output 334。40 Answer #include int main() { int m,x,n,a; float b; scanf(”%d%d%d",&m,&n,&x); 0 x m〈n&&n<1000; a=m*x; if (a>n) b=0。88*a; else b=a; printf(”%.2fn",b); return 0; } 9、 判断闰年 Description 输入一个正整数的年份,判断是否为闰年。 Input 输入只有一行,为一个10000以内的正整数。 Output 输出为一行. 若输入为闰年偶数则输出“Yes",否则输出“No”。 Sample Input 2010 Sample Output No 答案 #include int main() { int a; scanf(”%d",&a); if (a>0&&a〈10000) { if (a%4==0&&a%100!=0) printf(”Yesn"); else if (a%400==0) printf("Yesn”); else printf("Non"); } else printf(”error”); return 0; } 10、 水仙花数 Description 333 如果一个三位十进制数等于其各位数字的立方和,则称这个数为水仙花数.如:1+5+3=153. Input 一个整数x,100<=x〈=999。 Output x是水仙花数,则输出“YES”,否则为“NO”。 Sample Input 153 Sample Output YES Answer #include int main() { int a,b,c,d,e; scanf("%d",&a); b=a/100; c=(a—b*100)/10; d=(a—b*100-c*10); e=b*b*b+c*c*c+d*d*d; if(a==e) printf("YESn”); else printf(”NOn"); return 0; } 11、 三个数比较大小 Description 从键盘上输入0~100之间的三个数,按从小到大的顺序输出。 Input 输入只有一行,为三个整数。 Output 按从小到大输出这三个数。 Sample Input 15 10 20 Sample Output 10 15 20 Answer #include int main() { int a,b,c; scanf(”%d %d %d",&a,&b,&c); if (a〉=b) { if (b〉=c) printf("%d %d %dn”,c,b,a); else if (a>c) printf(”%d %d %dn”,b,c,a); else printf("%d %d %dn",b,a,c); } else { if (a〉=c) printf("%d %d %dn",c,a,b); else if (b〉=c) printf("%d %d %dn”,a,c,b); else printf(”%d %d %dn”,a,b,c); } return 0; } 12、 输出整数的最低两位 Description 把一个整数的最低两位打印出来,不输出整数的符号。 Input 输入为一个整数n,不会超出int类型的数据范围。 Output 输出n的最低两位数字。但是,输入的数字本身不足两位时,不应当补0。如,输入为“1”,则输出为“1"。 Sample Input -102 Sample Output 02 Answer #include int main() { int a,b,c; scanf("%d",&a); if(a〉=100) { b=a-a/100*100; printf(”%。2dn”,b); } else if(a>=0) { printf("%dn”,a); } else if(a〉=-99) { printf(”%dn",-a); } else { c=—a; b=c—c/100*100; printf("%。2dn",b); } return 0; } 13、判断奇偶数(填空) Description 编写一个程序,判断读取的正整数的奇偶性,部分程序已经给出,请填上空白语句,并提交填充后的完整程 序。 程序(含答案): #include int main() { int num; scanf("%d”,&num); if (num%2==0) printf(”%d is an even number.",num);//num是一个偶数 else printf(”%d is an odd number.",num);//num是一个奇数 return 0; } 14、求分段函数的值(填空) Description 设有分段函数如下: 给出N>0个x的值,求对应的y值并输出。 部分程序已经给出,请填充其中的空白语句,并提交填充后的完整程序. 程序(含答案): #include #include int main() { double x,y; int i,N; scanf(”%d",&N); for (i=0;i〈N;i++) { scanf("%lf”,&x); if (x〈0) y=-x; else if (x〈1) y=sin(2*x); else if (x<5) y=sqrt(x*x*x+x); else y=2*x+10; if (i==0) printf("case 1:y=%.6g。”,y); else printf("ncase %d:y=%。6g。",i+1,y); } return 0; } 15、输出是m的倍数或n的倍数、但不是m和n的公倍数的数 Description 输出1~k之间是m的倍数或n的倍数、但不是m和n的公倍数的数,其中1<=m,n〈k〈100,且m与n不相等. Input 输入三个整数,依次为k、m、 n. Output 从小到大输出符合题意的所有整数,两数之间用一个空格分开. Sample Input 15 2 3 Sample Output 2 3 4 8 9 10 14 15 Answer #include int main() { int k,m,n,a,i=1; scanf("%d %d %d”,&k,&m,&n); } if(m a=m; else a=n; printf("%d",a); for(i=a+1;i<=k;i++) { if((i%m==0&&i%n!=0)||(i%n==0&&i%m!=0)) printf(” %d",i); } return 0; 16、A+B ProblemT Description 计算a+b,0〈=a,b<1000。 Input 输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开. Output 每行输出一个a+b的值,顺序与输入对应。 Sample Input 1 2 10 20 Sample Output 3 30 Answer #include int main() { int a,b; while(scanf(”%d %d",&a,&b)!=EOF) { printf(”%dn”,a+b); } return 0; } 17、A+B Problem (II) : Input/Output Pratice Description 计算a+b,0〈=a,b<1000。 Input 输入的第一行是一个整数N,后面有N对整数a和b,每对a和b占一行,a,b用空格分开。 Output 每行输出一个a+b的和,顺序与输入对应. Sample Input 2 1 2 10 20 Sample Output 3 30 Answer #include int main() { int a[1000],b[1000],N,i; scanf("%d",&N); for(i=1;i<=N;i++) scanf("%d %d”,&a[i],&b[i]); for(i=1;i〈=N;i++) printf(”%dn",a[i]+b[i]); return 0; } 18、成绩的等级 Description 把百分制的考试成绩转换成五级制的成绩: 90~100:Excellent 80~89:Good 70~79:Average 60~69:Pass 0~59:Failing 不在0~100之间的输入是非法数据,输出“Error”。 Input 输入多行,每行一个整数. Output 输入所对应的成绩等级。 Sample Input —1 81 92 35 68 72 100 Sample Output Error Good Excellent Failing Pass Average Excellent Answer #include int main() { int score; while(scanf("%d”,&score)!=EOF) { if (score<0||score〉100) printf(”Errorn"); else { switch (score/10) { case 0: case 1: case 2: case 3: case 4: case 5:printf(”Failingn");break; case 6:printf(”Passn”);break; case 7:printf(”Averagen");break; case 8:printf(”Goodn");break; case 9: case 10:printf("Excellentn”);break; } } } return 0; } 19、n个数的最大值和最小值 Description 找出n个数中最大的数和最小的数,并将它们的值输出出来. Input 输入为n+1个整数,都在int类型范围内.这些数可能用若干空格或者换行符分隔开。 输入的第1个数为n,表示后续有n个数输入。从输入的第2个数开始,求出直到第n+1个数中最大的数和 最小的数。 Output 输出为两行,格式见sample。 Sample Input 3 0 1 —1 Sample Output The maximum number is 1. The minimum number is —1. Answer #include〈stdio。h〉 int main() { int n,i,max,min; scanf("%d",&n); int a[n]; for(i=0; i〈n; i++) scanf("%d",&a[i]); max=a[0]; min=a[0];
版权声明:本文标题:C语言练习题(山东科技大学吐血整理) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1713759485a650228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论