输出为两行,按每行的长度从长到短输出。 数学11-1 C语言平时训练题 1、算术基本运算
Description 计算两整数x和y(0 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 int x,y,a,b,c,d,e,f,g; 0 e=x%y; f=x*x; g=y*y*y; printf(\+ y : %d\\n\ printf(\- y : %d\\n\ printf(\* y : %d\\n\ printf(\/ y quotient: %d, remainder: %d\\n\ printf(\^ 2 : %d\\n\ printf(\ return 0; } 2、求圆的面积和周长 Description 从键盘输入圆的半径,求圆的面积和周长,圆周率取3.14。 Input 输入一个浮点型数据,有效数字不会超过十进制的6位。 Output 输出为两行。第一行为圆的面积,第二行为圆的周长,格式见sample。 Sample Input 3 Sample Output Area: 28.260000 Perimeter: 18.840000 Answer #include float r,s,c; scanf(\ s=PI*r*r; c=2*PI*r; printf(\ printf(\ return 0; } 3、平均值 Description 求3个数的平均值。 Input 输入只有一行,为3个较小的整数。 Output 输出为这3个整数的平均值,保留3位小数。 Sample Input 1 2 3 Sample Output 2.000 Answer #include { int a,b,c; float d; scanf(\%d %d\ d=(a+b+c)/3.0; printf(\ return 0; } 4、货币兑换 Description 给出人民币对美元、欧元、日元的当日汇率,求给定金额的人民币能兑换成外币的金额,求给定金额的外币能兑换成人民币的金额。要计算的外币有三种:美元、欧元、日元。 Input 输入有三行。第一行依次为美元、欧元、日元外币汇率,用空格分开。汇率用100 外币为单位,精确到小数点后4位,如668.5200表示“100美元=668.5200人民币 ”。汇率浮动范围为(0,10000)。第二行为外币金额x,第三行为人民币金额y。x,y均为整数,且0 Sample Output 10027.80 13621.03 119.78 224.38 165.19 18784.75 Answer #include scanf(\ scanf(\ scanf(\ 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(\ printf(\ 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 char a,b,c; scanf(\ printf(\%.3o %.3x\\n\ printf(\%.3o %.3x\\n\ printf(\%.3o %.3x\\n\ return 0; } 6、奇数还是偶数? Description 输入一个整数,判读它是奇数还是偶数。 Input 输入只有一行,为一个100以内的正整数。 Output 输出为一行。若输入为偶数则输出“even”,奇数输出“odd”。 Sample Input 30 Sample Output even Answer #include int a; scanf(\ if(a>=0&&a<=100) { if (a%2==0) printf(\ else printf(\ } else printf(\ return 0; } 7、绝对值 Description 求整型数据和浮点型数据的绝对值。 Input 输入两个数,第一个是整数,第二个是浮点数。 Output 输出为两行,第一行为整数的绝对值,第二行为浮点数的绝对值,注意浮点数的绝对值不输出无意义的0。 Sample Input -1 1 Sample Output 1 1 Answer #include int main() { int a,c; double b,d; scanf(\ c=abs(a); d=fabs(b); printf(\ return 0; } 8、简单的打折计算 Description 商店规定:消费满n元,可以打八八折。设某件商品标价m元,输入购买的件数x,计算出需要支付的金额(单位:元),精确到分。 Input 输入只有一行,三个整数m、n和x,且0 Answer #include int m,x,n,a; float b; scanf(\ 0 b=0.88*a; else b=a; printf(\ return 0; } 9、判断闰年 Description 输入一个正整数的年份,判断是否为闰年。 Input 输入只有一行,为一个10000以内的正整数。 Output 输出为一行。若输入为闰年偶数则输出“Yes”,否则输出“No”。 Sample Input 2010 Sample Output No 答案 #include scanf(\ if (a>0&&a<10000) { if (a%4==0&&a0!=0) printf(\ else if (a@0==0) printf(\ else printf(\ } else printf(\ return 0; } 10、水仙花数 Description 如果一个三位十进制数等于其各位数字的立方和,则称这个数为水仙花数。如:13+53+33=153 。 Input 一个整数x,100<=x<=999 。 Output x是水仙花数,则输出“YES”,否则为“NO”。 Sample Input 153 Sample Output YES Answer #include a,b,c,d,e; scanf(\ 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(\ else printf(\ return 0; } int m,n,i,j,k,t; scanf(\ for(i=n;i>=m;i--) { t=0; for(j=2;j<=sqrt(i);j++)//12 if(i%j==0) t=1; if(t==0&&i>1) printf(\ } printf(\ 22、 Sum Problem (II) : Input/Output Pratice Description 计算若干整数的和,这些整数都是小于1000的非负整数。 Input 输入的第一行是一个整数M,后面有M个测试样例。每个测试样例以一个整数N开始,后面接着是N个整数。 Output 每组测试样例对应一行输出,为所给的N个整数之和,顺序与输入对应。 Sample Input 2 3 1 2 3 5 10 15 20 30 50 Sample Output 6 125 Answer #include for(i=1;i<=m;i++) { scanf(\ s=0; for(j=1;j<=n;j++) { scanf(\ s=s+a; } printf(\ } return 0; } 23、十进制整数转二进制 Description 给出一个十进制的非负整数x,x<=216,把它转换成二进制数输出。 Input 输入为多行,每行一个整数x,至读入EOF结束。 Output 每行输出x对应的二进制数值。 Sample Input 0 1 3 33 65535 Sample Output 0 1 11 100001 1111111111111111 Answer #include int a[100],i,b; while(scanf(\ { for(i=0;;i++) { a[i]=b%2; b=b/2; if(b==0) break; } for(;i>=0;) { printf(\ i--; } printf(\ } return 0; } 24、简单的整数排序 Description 对给出的若干整数按从小到大排序。 Input 输入的第一个数为n(n<=1000),后接n个整数。 Output 按从小到大的顺序输出这些整数,每两个整数之间用一个空格分隔开,最后一个整数后面没有空格。 Sample Input 10 3 9 1 5 2 8 5 6 7 3 Sample Output 1 2 3 3 5 5 6 7 8 9 Answer #include { if(a[j]>a[j+1]) { c=a[j]; a[j]=a[j+1]; a[j+1]=c; } } } printf(\ for(i=1;i 注意:主函数已经给出,只需提交strlen()函数及必要的头文件包含命令。 Input 输入为多行。第一行N>0表示有N个测试用例,后面有N行,每行包含一个字符串(不超过1000个字符)。 Output 输出为多行,每行对应于一个测试用例。每行的格式为: case i:lenght=j. 其中i表示测试用例编号(从1开始),j表示相应的字符串长度。 Sample Input 4 I love China! Do you want to pass this examination? You will succeed finially! Wish you succeed! Sample Output case 1:length=13. case 2:length=37. case 3:length=26. case 4:length=17. Answer #include int j; for(j=0;str[j]!='\\0';j++) { } return j; } int main() { int i,N; char str[1001]; scanf(\ getchar(); gets(str); printf(\1:length=%d.\ for (i=2;i<=N;i++) { gets(str); printf(\ } return 0; } 33、求数组中的最大值(填空) Description 现有一个不超过1000个整数组成的数组,其中可能有重复数据出现。要求编写一个程序,求该数组中的最大值以及最大值所在的所有下标。部分程序已经给出,请填充空白语句。程序(含答案): #include max=array[0]; numOfMax=0; maxIndex[numOfMax++]=0; for (i=1; i is %d, whose positions are:\ printf(\ for (i=1;i 行N列的矩阵。之后是一个M行N列的整数组成的矩阵。 Output 输出有K行,每个测试用例的结果占一行。每行的格式为: case i:d1 d2 ... dj 其中i表示测试用例的编号(从1开始),d1、d2、....、dj表示相应测试用例的各行的和,两两之间用空格隔开。 Sample Input 4 3 3 1 2 3 1 2 3 1 2 3 2 3 1 1 1 1 1 1 1 1 1 5 1 3 4 5 6 7 Sample Output case 1:6 6 6 case 2:3 3 case 3:1 case 4:3 4 5 6 7 Answer #include int K,i,j,k,m,n,r[100],sum[100][100],a[100][100]; scanf(\ for(k=0;k scanf(\ sum[k][i]=sum[k][i]+a[i][j]; } } } for(k=0;k Sample Output case 1:1 2 3 4 5 6 7 8 9 10 case 2: case 3:5 4 3 2 1 Answer #include for(i=0;i for(j=M[i]-1;j>0;j--) printf(\\ printf(\ printf(\ } else printf(\%d:\\n\ } return 0; } 36、结构体的使用(编程题) Description 设有结构体定义如下: typedef struct Student {char major[50];//专业 char name[50];//姓名 int score[3];//3门课程的成绩 } STU; 编写一个子函数,输出每个学生的总分,函数原型如下: void printInfo(STU students[],int num); 其中students[]是由num个STU类型的结构体组成的数组。输出格式见下。注意:主函数已经给出,提交时需提交以下内容:上述结构体STU的定义(直接复制上就可以,要放在头文件包含命令之后)、必要的头文件包含命令以及printInfo函数的代码。 Input 输入为多行。第一行N>0表示有N个学生的信息。之后有N行,每一行包含5个部分,分别表示每位学生的专业、姓名和3门课程的成绩,两两之间用空格隔开。成绩为正整数。 Output 输出为N行,每一行为一名学生的信息,格式为: major,name:totalSocre. 其中major表示学生的专业,name表示学生的姓名,totalScore表示该生的总分。所有的标点符号均为半角字符。 Sample Input 3 Computer Tom 100 98 89 Information Jack 98 89 87 Management Mary 89 89 89 Sample Output Computer,Tom:287. Information,Jack:274. Management,Mary:267. Answer #include s[i].score[1]+students[i].score[2]); printf(\} } int main() { int i,N; scanf(\ STU stus[N]; getchar(); for (i=0;i