【C】将三个数按从大到小输出

来源:互联网 发布:淘宝aj比较靠谱的店 编辑:程序博客网 时间:2024/05/16 01:10

将三个数按从大到小输出。

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int arr[3];  
  5.     int a, b, c;  
  6.     for(a = 0; a < 3; a++)  
  7.     {  
  8.         scanf_s("%d", &arr[a]);  
  9.     }  
  10.     for(b = 0; b < 3; b++)  
  11.     {  
  12.         for(a = 0; a < 3 - b; a+  
  13.                 arr[a+ 1] = c;  
  14.             }  
  15.         }  
  16.     }  
  17.     for(a = 0; a < 3; a++)+)  
  18.         {  
  19.             if(arr[a] < arr[a + 1])  
  20.             {  
  21.                 c= arr[a];  
  22.                 arr[a]= arr[a + 1];  
  23.     {  
  24.         printf("%d ",arr[a]);  
  25.     }  
  26.     system("pause");  
  27.     return 0;  
  28.    
  29. }  

另一种程序:

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int a, b, c;  
  5.     scanf_s("%d", &a);  
  6.     scanf_s("%d", &b);  
  7.     scanf_s("%d", &c);  
  8.     if (a > b)  
  9.     {  
  10.         if (a > c)  
  11.         {  
  12.             if (b > c)  
  13.             {  
  14.                 printf("%d,%d,%d", a, b, c);  
  15.             }  
  16.             else  
  17.             {  
  18.                 printf("%d,%d,%d", a, c, b);  
  19.             }  
  20.         }  
  21.         else  
  22.         {  
  23.             printf("%d,%d,%d", c, a, b);  
  24.         }  
  25.     }  
  26.     else  
  27.     {  
  28.         if (a > c)  
  29.         {  
  30.             printf("%d,%d,%d", b, a, c);  
  31.         }  
  32.         else  
  33.         {  
  34.             if (b > c)  
  35.             {  
  36.                 printf("%d,%d,%d", b, c, a);  
  37.             }  
  38.             else  
  39.             {  
  40.                 printf("%d,%d,%d", c, b, a);  
  41.             }  
  42.         }  
  43.     }  
  44.     system("pause");  
  45.     return 0;  
  46. }  


修正后的程序:

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. void Swap(int* px, int* py)  
  3. {  
  4.     int tmp = *px;  
  5.     *px = *py;  
  6.     *py = tmp;  
  7. }  
  8.   
  9. int main()  
  10. {  
  11.     int a=2,b=14,c=3;  
  12.     if(a<b)  
  13.     {  
  14.         Swap(&a, &b);  
  15.     }  
  16.     if(a<c)  
  17.     {  
  18.         Swap(&a, &c);  
  19.     }  
  20.     if(b<c)  
  21.     {  
  22.         Swap(&b, &c);  
  23.     }  
  24.     printf("%d %d %d\n", a,b,c);  
  25.     system("pause");  
  26.     return 0;  
  27. }  


0 0