2000ASCII码排序
这题超简单的,说白了就是给三个数按从小到大的顺序排序。
代码展示:
#include<stdio.h> void main() { char ch1,ch2,ch3,temp; while(scanf("%c%c%c",&ch1,&ch2,&ch3)!=EOF) { getchar(); if(ch1>ch2) { temp=ch1; ch1=ch2; ch2=temp; } if(ch1>ch3) { temp=ch1; ch1=ch3; ch3=temp; } if(ch2>ch3) { temp=ch2; ch2=ch3; ch3=temp; } printf("%c%2c%2c\n",ch1,ch2,ch3); } }
2001计算两点间距离关键在于将坐标及计算得到的距离均声明为double型double x1,y1,x2,y2,dist;还有输出格式有限制,要求是保留小数点后两位小数printf("%.2lf\n",dist);
#include<stdio.h> #include<math.h> int main() { double x1,y1,x2,y2,dist; while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2)!=EOF) { dist=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); printf("%.2lf\n",dist); } return 0; }2002计算球体积
#include<stdio.h> #include<math.h> #define PI 3.1415927 void main() { double r,s; while(scanf("%lf",&r)!=EOF) { s=(double)(4*PI*pow(r,3))/3; printf("%.3lf\n",s); } }
2003求绝对值
#include<stdio.h> #include<math.h> void main() { double num; while(scanf("%lf",&num)!=EOF) { printf("%.2lf\n",fabs(num)); } }以上2001——2003均调用了math.h中的数学函数,pow()求指数函数的值;sprt()求开根号的值;fabs()是求绝对值。
2004成绩交转换
是对switch(){case……}的应用。#include<stdio.h> void main() { int n; double t; while(scanf("%lf",&t)!=EOF) { if(t>=90&&t<=100) n=1; else if(t>=80&&t<=89) n=2; else if(t>=70&&t<=79) n=3; else if(t>=60&&t<=69) n=4; else if(t>=0&&t<=59) n=5; else n=0; switch(n) { case 0:printf("Score is error!\n");break; case 1:printf("A\n");break; case 2:printf("B\n");break; case 3:printf("C\n");break; case 4:printf("D\n");break; case 5:printf("E\n");break; } } }