杭电oj2000-2011

2000  ASCII码排序

 1 #include <stdio.h>
 2 
 3 int main(){
 4     char a,b,c,t;
 5     while(scanf("%c%c%c", &a, &b, &c)!=EOF){
 6         getchar();#必须得加这玩意
 7         if(a>b){
 8             t = a;
 9             a = b;
10             b = t;
11         }
12         if(a>c){
13             t = a;
14             a = c;
15             c = t;
16         }
17         if(b>c){
18             t = b;
19             b = c;
20             c = t;
21         }
22         printf("%c %c %c\n", a,b,c);
23     }
24 }

一开始没有添加getchar()函数引发错误,后来查看大佬的wp知道了原因这里引用大佬的解释:

第一次没有加getchar()函数,结果不正确,运行时,第一次输入三个字符排序正确,可是后面再输入时输出就会错误。通过查看别人类似问题的分析,知道了其中缘由。从键盘输入的字符,会存放到缓冲区中,包括回车符。如输入“abc回车”之后,缓冲区中存了有四个字符'a'、'b'、'c'、'\n',如果上面程序中没有getchar(),则第一次从缓冲区中提取了三个字符之后,还剩下了一个'\n',则下次再输入三个字符时,缓冲区中会把上次剩下的回车符也算进去,这样处理起来就会有问题。getchar()是从缓冲区中读取一个字符,如果只是去除缓冲区中的字符,不需要用赋值表达式,直接使用getchar();即可,当然,使用赋值表达式也可以,如:m=getchar();。上面程序中使用了getchar()之后,就可以把每次输入三个字符之后的回车符吸收掉,这样结果就正确了。   

 

 

2001  计算两点间的距离

 1 #include <stdio.h>
 2 #include <math.h>
 3 int main(){
 4     double x1,y1,x2,y2,d;
 5     while(scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2)!=EOF){
 6         d= sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
 7         printf("%.2lf\n", d);
 8 
 9     }
10 }

 

2002  计算球体积

 1 #include <stdio.h>
 2 #define PI 3.1415927
 3 
 4 int main(){
 5     double x, v;
 6     while(scanf("%lf", &x)!=EOF){
 7         v = 4*PI*x*x*x/3;
 8         printf("%.3lf\n", v);
 9     }
10 }

 

2003  求绝对值

 1 #include <stdio.h>
 2 
 3 int main(){
 4     double x,y;
 5     while(scanf("%lf", &x)!=EOF){
 6         y = x;
 7         if(y<0){
 8             y = -x;
 9         }
10         printf("%.2lf\n", y);
11     }
12 }

 

2004  求绝对值

 1 #include <stdio.h>
 2 
 3 int main(){
 4     int x;
 5     while(scanf("%d", &x)!=EOF){
 6         if(x<0 || x>100){
 7             printf("Score is error!\n");
 8         }
 9         if(x>=0 && x<60){
10             printf("E\n");
11         }
12         if(x>=60 && x<70){
13             printf("D\n");
14         }
15         if(x>=70 && x<80){
16             printf("C\n");
17         }
18         if(x>=80 && x<90){
19             printf("B\n");
20         }
21         if(x>=90 && x<=100){
22             printf("A\n");
23         }
24     }
25 }

 

2005  第几天?

 1 #include <stdio.h>
 2 int main(){
 3     int a,b,c,i,days;
 4     int mon[12] = {31,0,31,30,31,30,31,31,30,31,30,31};
 5     while(scanf("%d/%d/%d", &a,&b,&c)!=EOF){
 6         if((a%4==0 && a%100 !=0) || (a%400 == 0)){
 7             days = 0;
 8             mon[1] = 29;
 9             for(i=0; i<b-1; i++){
10                 days += mon[i];
11             }
12             days += c;
13             printf("%d\n", days);
14         }else{
15             days = 0;
16             mon[1] = 28;
17             for(i=0; i<b-1; i++){
18                 days += mon[i];
19             }
20             days += c;
21             printf("%d\n", days);
22         }
23     }
24 
25 }

 

2006  求奇数的乘积

 

#include <stdio.h>
int main(){
    int n, i ,mul;
    while((scanf("%d", &n))!=EOF){
        mul = 1;
        while(n--){
            scanf("%d", &i);
            if(i%2!=0){
                mul = mul*i;
            }
        }
        printf("%d\n", mul);
    }

}

 

2007  平方和与立方和

这玩意首先要判断输入两个数的大小,不然就是错的

 1 #include <stdio.h>
 2 int main(){
 3     int n,m,squ,cub,i;
 4     while((scanf("%d %d", &n,&m))!=EOF){
 5         squ=0;
 6         cub=0;
 7         if(n<m){
 8             for(i=n;i<=m;i++){
 9                 if(i%2==0){
10                     squ += i*i;
11                 }else{
12                     cub += i*i*i;
13                 }
14             }
15             printf("%d %d\n",squ,cub);
16         }else{
17             for(i=m;i<=n;i++){
18                 if(i%2==0){
19                     squ += i*i;
20                 }else{
21                     cub += i*i*i;
22                 }
23             }
24             printf("%d %d\n",squ,cub);
25         }
26 
27     }
28 
29 }

 

 

2008  数值统计

 1 #include <stdio.h>
 2 int main(){
 3     int n,x,y,z;
 4     double i;
 5     while((scanf("%d", &n))!=EOF){
 6         if(n==0){
 7             continue;
 8         }else{
 9             x=0;
10             y=0;
11             z=0;
12             while(n--){
13                 scanf("%lf", &i);
14                 if(i<0){
15                     x += 1;
16                 }else if(i==0){
17                     y += 1;
18                 }else if(i>0){
19                     z += 1;
20                 }
21             }
22             
23             
24         }
25         printf("%d %d %d\n", x,y,z);
26     }
27     return 0;
28 }

 

2009  求数列的和

 1 #include <stdio.h>
 2 #include <math.h>
 3 int main(){
 4     int m;
 5     double i,sum,n;
 6     while(scanf("%lf %d", &n,&m)!=EOF){
 7         sum = 0;
 8         while(m--){
 9             sum += n;
10             n = sqrt(n);
11             
12         }
13         printf("%.2lf\n", sum);
14     }
15 }

 

2010  水仙花数

最后输出的时候最后不允许有空格,因此第一个数的输出与后面数的输出不一样。

 1 #include <stdio.h>
 2 
 3 int main(){
 4     int m,n,a,b,c,i,flag;
 5     while(scanf("%d %d",&m,&n)!=EOF){
 6         flag = 0;
 7         for(i=m;i<=n;i++){
 8             a=i/100;
 9             b=i%100/10;
10             c=i%10;
11             if(i==a*a*a+b*b*b+c*c*c){
12                 flag += 1;
13                 if(flag==1){
14                     printf("%d",i);
15                 }else{
16                     printf(" %d",i);
17                 }
18                 
19             }
20         }
21         if(flag==0){
22             printf("no");
23         }    
24         printf("\n");
25         
26     }
27 }

 

 2011  多项式求和

 1 #include <stdio.h>
 2 
 3 int main(){
 4     int n,i;
 5     double a,sum,j;
 6     scanf("%d", &n);
 7     while(n--){
 8         sum = 0;
 9         scanf("%lf",&a);
10         for(i=1;i<=a;i++){
11             if(i%2==0){
12                 j = i;
13                 sum -= 1/j;
14             }else{
15                 j = i;
16                 sum += 1/j;
17             }
18         }
19         printf("%.2lf\n", sum);
20     }
21 }

 

 

 

 

posted @ 2019-02-20 20:43  Ragd0ll  阅读(743)  评论(0编辑  收藏  举报