把百分制的考试成绩转换成五级制的成绩: 90~100:Excellent 80~89:Good 70~79:Average 60~69:Pass 0~59:Failing
不在0~100之间的输入是非法数据,输出“Error”。 Input
输入多行,每行一个整数。 Output
输入所对应的成绩等级。 #include
int n;
while (scanf (\ {
if (n==100)
printf (\ else if(n<0||n>100) printf (\ else
switch (n/10)
{case 9:printf (\ case 8:printf (\ case 7:printf (\ case 6:printf (\ default :printf (\
} }
return 0; }
/////Problem E: 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
HINT
N给出了测试样例数,用for循环处理方便。 #include
int N,i,j;
scanf(\ int a[N][2];
for (i=0;i for (j=0;j<2;j++) scanf (\ } for (i=0;i printf (\} Problem A: A+B Problem (III) : Input/Output Pratice Description 计算a+b,0<=a,b<1000。 Input 输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开。当测试样为0 0时表示输入结束,0 0不参与运算。 Sample Input 1 2 10 20 0 0 Sample Output 3 30 #include scanf(\ if(a==0&&b==0) break; else printf(\ } } Problem C: 只有一个二元运算符的表达式运算 Description 编程序读入并计算只有一个二元运算符的表达式的值。用到的二元运算符有:“+”、“-”、“*”、“/”、“%”,与C语言的语法一致。 Input 每行输入一个表达式,格式为:二个整型的操作数a和b,中间用一个符号分开,这个符号就是运算符。测试样例不存在除数为0的情况。 输入以a和b为0,且用一个空格分开结束。 Output 每行对应输入的运算符为“+”、“-”、“*”、“/”、“%”,则计算a+b、a-b、a*b、a/b、a%b的值;否则输出“invalid op”。 HINT 教材上有非常相似的例题可以参考。 #include int a,b; char i; while(1) {scanf (\ if(a==0&&b==0&&i==32) break; else {switch(i) { case '+':printf(\ case '-':printf(\ case '*':printf(\ case '/':printf(\ case '%':printf(\ default:printf(\ } } } } Problem D: 求100以内的素数 Description 素数是只能被1和自身整除的正整数,根据数学定义1不是素数。素数也叫质数。 Input 输入为两个整数m和n,满足0<=m<=n<=100。 Output 从大到小输出m~n之间的所有素数,一个素数一行。如果m~n之间没有素数,则不输出任何数。 输出的所有数在两行“=====”之间。 Sample Input 2 12 Sample Output ===== 11 7 5 3 2 ===== HINT 利用素数的数学规律可以很容易的解出此题,题目给出的数据范围是关键。 #include int m,n,i,j,k; scanf(\ printf(\ for(i=n;i>=m;i--) { k=0; for(j=1;j printf(\ } printf(\} Problem E: 十进制整数转二进制 Description 给出一个十进制的非负整数x,x<=216,把它转换成二进制数输出。 Input 输入为多行,每行一个整数x,至读入EOF结束。 Output 每行输出x对应的二进制数值。 #include int i,j,m,n,k; int a[17]; while(scanf(\ {if(n==0) printf(\ else { for(m=0;m<=16;m++) { i=n%2; j=n/2; n=j; a[m]=i; } for(m=16;m>=0;m--) { if(a[m]==1) {k=m; break;} } for(m=k;m>=0;m--) printf(\ printf(\ } } } /////Problem F: 辗转相除法 最大公约数的算法 Description 辗转相除法,也称欧几里得算法,是求最大公约数的算法。Input 输入为多行,每行有一对非负整数a,b,且a*b不会超出int类型的数据范围。输入至EOF结束。 Output 每行输出一对a,b的最大公约数和最小公倍数,顺序与输入对应。 从数论上的整除定义出发:若a整除b(b除以a没有余数),则b是a的倍数,a是b的约数,这里要求b不为0。因此0是任意整数的倍数,但是0不能是约数。 #include { int a,b,c,m,t; while(scanf(\ { if(a==0&&b!=0) printf(\ else if(a!=0&&b==0) printf(\ else { if(a while(c!=0) { a=b; b=c; c=a%b; } printf(\