笨方法分成绩等级。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int score;
char grade;
printf("please input score:");
scanf("%d",&score);
if (score >= 90 && score <= 100)
grade = 'A';
if (score >= 80 && score <= 90)
grade = 'B';
if (score >= 70 && score <= 80)
grade = 'C';
if (score >= 60 && score <= 70)
grade = 'D';
if (score >= 0 && score <= 60)
grade = 'E';
if (score >= 100 || score <= 0)
printf("Input error!\n");
else
printf("grade:%c\n",grade);
return 0;
}
简洁一级。
int score;
char grade;
printf("please input score:");
scanf("%d",&score);
if (score >= 100 || score < 0)
{
printf("Input error!\n");
}
else
{
if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'E';
printf("grade:%c\n",grade);
}
return 0;
简洁二级。
int score;
char grade;
printf("please input score:");
scanf("%d",&score);
if (score >= 100 || score < 0)
printf("Input error!\n");
else if(score >= 90)
printf("grade:A\n",grade);
else if (score >= 80)
printf("grade:B\n",grade);
else if (score >= 70)
printf("grade:C\n",grade);
else if (score >= 60)
printf("grade:D\n",grade);
else
printf("grade:E\n",grade);
return 0;
简洁三级。
int main()
{
int score,mark;
printf("please input score:");
scanf("%d",&score);
mark = score>=0&&score<=100 ? score / 10 : -1;
switch (mark)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:printf("garde:E\n");
break;
case 6:printf("garde:D\n");
break;
case 7:printf("garde:C\n");
break;
case 8:printf("garde:B\n");
break;
case 9:
case 10:printf("garde:A\n");
break;
default:printf("Input error!\n");
}
return 0;
从键盘输入一个字符,编程序输出该字符及其ASCII码。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
printf("Please enter a letter:");
scanf("%c",&c);
printf("%c %d",c,c);
return 0;
}
输入大写字母,输出小写字母。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a,b;
a = getchar();
b = a + 32;
printf("%c\n",b);
return 0;
}
已知一位小朋友的电影票价是10元,计算x位小朋友的总票价是多少?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,y;
scanf("%d",&x);
y = 10 * x;
printf("%d %d",x,y);
return 0;
}
输入两个正整数a和b,试交换a,b的值。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c;
scanf("%d %d",&a,&b);
c = a;
a = b;
b = c;
printf("%d %d\n",a,b);
return 0;
}
输入底面半径为r和高h,输出圆柱体的表面积,保留三位小数。定义pi=3.1415926
#include <stdio.h>
#include <stdlib.h>
int main()
{
double r,h,s,pi;
scanf("%lf %lf",&r,&h);
pi = 3.1415926;
s = pi * r * r * 2 + 2 * pi * r * h;
printf("%.3lf",s);
return 0;
}
整型数据类型存储空间大小。
分别定义int,short类型的变量各一个,并依次输出它们的储存空间大小(单位:字节)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
short b;
printf("%d %d",sizeof(a),sizeof(b));
return 0;
}