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()以後,就能夠把每次輸入三個字符以後的回車符吸取掉,這樣結果就正確了。 spa
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 }
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 }
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 }
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 }
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 }
#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); } }
這玩意首先要判斷輸入兩個數的大小,否則就是錯的code
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 }
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 }
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 }
最後輸出的時候最後不容許有空格,所以第一個數的輸出與後面數的輸出不同。blog
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 }
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 }