返回列表 發帖

三數比大小 (不考慮兩數或三數相等的情況)

試設計一個小程式, 讓使用者任意輸入三個數, 接著電腦回應出這三個數的大小順序, 由大而小排列出.
譬如:
當使用者依序輸入5, 12, 3, 電腦會回應 "12>5>3"
當使用者依序輸入7, 2, 11, 電腦會回應 "11>7>2"
當使用者依序輸入7, 7, 11, 電腦會回應 "其中兩個數, 或三個數相等!"
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     re:
  7.     int x,y,z;
  8.     cout<<"請任意輸入三個數:";
  9.     cin>>x>>y>>z;
  10.    
  11.     if(x>y && y>z)
  12.     {
  13.         cout<<x<<">"<<y<<">"<<z<<endl; //x>y>z
  14.     }else if(x>z && z>y)
  15.     {
  16.         cout<<x<<">"<<z<<">"<<y<<endl; //x>z>y
  17.     }else if(y>z && z>x)
  18.     {
  19.         cout<<y<<">"<<z<<">"<<x<<endl; //y>z>x
  20.     }else if(y>x && x>z)
  21.     {
  22.         cout<<y<<">"<<x<<">"<<z<<endl; //y>x>z
  23.     }else if(z>x && x>y)
  24.     {
  25.         cout<<z<<">"<<x<<">"<<y<<endl; //z>x>y
  26.     }else if(z>y && y>x)
  27.     {
  28.         cout<<z<<">"<<y<<">"<<x<<endl; //z>y>x
  29.     }else
  30.     {
  31.         cout<<"其中兩個數, 或三個數相等!"<<endl;
  32.     }
  33.     goto re;
  34.     system("pause");
  35.     return 0;
  36. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a,b,c;
  7.     cout<<"請輸入3個整數: ";
  8.     cin>>a>>b>>c;
  9.      if(a>b && b>c)
  10.        cout<<a<<">"<<b<<">"<<c<<endl;
  11.      else if(a>c && c>b)
  12.        cout<<a<<">"<<c<<">"<<b<<endl;
  13.      else if(b>a && a>c)
  14.        cout<<b<<">"<<a<<">"<<c<<endl;
  15.      else if(b>c && c>a)
  16.        cout<<b<<">"<<c<<">"<<a<<endl;
  17.      else if(c>a && a>b)
  18.        cout<<c<<">"<<a<<">"<<b<<endl;
  19.      else if(c>b && b>a)
  20.        cout<<c<<">"<<b<<">"<<a<<endl;
  21.      else
  22.      cout<<"其中兩個數, 或三個數相等!"<<endl;
  23.   system("pause");
  24.   return 0;   
  25. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     re:
  7.     int a,b,c;
  8.     cout<<"請任意輸入三個數:";
  9.     cin>>a>>b>>c;
  10.    
  11.     if(a>b && b>c)
  12. {
  13.         cout<<a<<">"<<b<<">"<<c<<endl;
  14. }else if(a>c && c>b)
  15. {
  16.       cout<<a<<">"<<c<<">"<<b<<endl;
  17. }else if(b>c && c>a)
  18. {
  19. cout<<b<<">"<<c<<">"<<a<<endl;
  20. }else if(b>a && a>c)
  21. {
  22.    cout<<b<<">"<<a<<">"<<c<<endl;
  23. }else if(c>a && a>b)
  24. {
  25.         cout<<c<<">"<<a<<">"<<b<<endl;
  26. }else if(c>b && b>a)
  27. {
  28.     cout<<c<<">"<<b<<">"<<a<<endl;
  29. }else
  30. {
  31.    cout<<"其中兩個數, 或三個數相等!"<<endl;
  32. }
  33.     goto re;
  34.     system("pause");
  35.     return 0;
  36. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y,z;
  7.     {
  8.        cout<<"請輸入三個值:";
  9.        cin>>x>>y>>z;
  10.        if(x>y&&y>z)
  11.        {
  12.          cout<<x<<">"<<y<<">"<<z;  
  13.        }  
  14.        else if(x>z&&z>y)
  15.        {
  16.          cout<<x<<">"<<z<<">"<<y;   
  17.        }
  18.        else if(y>z&&z>x)
  19.        {
  20.          cout<<y<<">"<<z<<">"<<x;   
  21.        }
  22.        else if(y>x&&x>z)
  23.        {
  24.          cout<<y<<">"<<x<<">"<<z;   
  25.        }
  26.        else if(z>x&&x>y)
  27.        {
  28.          cout<<z<<">"<<x<<">"<<y;   
  29.        }
  30.        else if(z>y&&y>x)
  31.        {
  32.          cout<<z<<">"<<y<<">"<<x;   
  33.        }   
  34.        else
  35.        {
  36.          cout<<"其中兩個數, 或三個數相等!"<<endl;   
  37.        }
  38.    
  39.     }
  40.     system("pause");
  41.     return 0;
  42.         
  43. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.     int a,b,c;
  8.     cout<<"請輸入三個數:"<<endl;
  9.     cin>>a>>b>>c;
  10.     if(a>b && b>c)                  
  11.     {
  12.         cout<<a<<">"<<b<<">"<<c<<endl;   
  13.     }else if(b>a && a>c)            
  14.     {
  15.         cout<<b<<">"<<a<<">"<<c<<endl;
  16.     }else if(c>a && a>b)            
  17.     {
  18.         cout<<c<<">"<<a<<">"<<b<<endl;
  19.     }else if(a>c && c>b)            
  20.     {
  21.         cout<<a<<">"<<c<<">"<<b<<endl;
  22.     }else if(b>c && c>a)            
  23.     {
  24.         cout<<b<<">"<<c<<">"<<a<<endl;
  25.     }else if(c>b && b>a)            
  26.     {
  27.         cout<<c<<">"<<b<<">"<<a<<endl;
  28.     }else
  29.     {
  30.          cout<<"其中兩個或三個數相等"<<endl;
  31.     }
  32.    
  33.     system("pause");
  34.     return 0;
  35. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y,z;
  7.     cout<<"請輸入三個數:";
  8.     cin>>x>>y>>z;
  9.    
  10.     if(x>y && y>z)
  11.     {
  12.         cout<<x<<">"<<y<<">"<<z<<endl;
  13.     }else if(x>z && z>y)
  14.     {
  15.         cout<<x<<">"<<z<<">"<<y<<endl;
  16.     }else if(y>z && z>x)
  17.     {
  18.         cout<<y<<">"<<z<<">"<<x<<endl;
  19.     }else if(y>x && x>z)
  20.     {
  21.         cout<<y<<">"<<x<<">"<<z<<endl;
  22.     }else if(z>x && x>y)
  23.     {
  24.         cout<<z<<">"<<x<<">"<<y<<endl;
  25.     }else if(z>y && y>x)
  26.     {
  27.         cout<<z<<">"<<y<<">"<<x<<endl;
  28.     }else
  29.     {
  30.         cout<<"其中兩個數, 或三個數相等!"<<endl;
  31.     }
  32.     system("pause");
  33.     return 0;
  34. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a,b,c,num;
  7.     re:
  8.     cout<<"請輸入三個數字:";
  9.     cin>>a>>b>>c;
  10.     if(a==b || b==c || c==a)
  11.     {
  12.         cout<<"其中兩數或三數相等";        
  13.     }
  14.     else
  15.     {
  16.         if(a>b)
  17.         {
  18.             if(c>a)
  19.             {
  20.                 cout<<c<<">"<<a<<">"<<b<<endl;      
  21.             }
  22.             else
  23.             {
  24.                 if(b>c)
  25.                 {
  26.                     cout<<a<<">"<<b<<">"<<c<<endl;
  27.                 }
  28.                 else
  29.                 {
  30.                     cout<<a<<">"<<c<<">"<<b<<endl;
  31.                 }     
  32.             }
  33.         }
  34.         else
  35.         {
  36.             if(c>b)
  37.             {
  38.                 cout<<c<<">"<<b<<">"<<a<<endl;      
  39.             }
  40.             else
  41.             {
  42.                 if(a>c)
  43.                 {
  44.                     cout<<b<<">"<<a<<">"<<c<<endl;
  45.                 }
  46.                 else
  47.                 {
  48.                     cout<<b<<">"<<c<<">"<<a<<endl;
  49.                 }     
  50.             }
  51.         }   
  52.     }
  53.     goto re;
  54.     system("pause");
  55.     return 0;   
  56. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a,b,c,max;
  7.     re:
  8.     cout<<"請輸入三個數";
  9.     cin>>a>>b>>c;
  10.     if(a>b&&b>c)            //abc
  11.     {
  12.        cout<<a<<">"<<b<<">"<<c<<endl;   
  13.     }
  14.     else if(a>c&&c>b)                   //acb
  15.     {
  16.        cout<<a<<">"<<c<<">"<<b<<endl;   
  17.     }
  18.     else if(b>a&&a>c)                   //bac
  19.     {
  20.        cout<<b<<">"<<a<<">"<<c<<endl;   
  21.     }
  22.     else if(b>c&&c>a)                   //bca
  23.     {
  24.        cout<<b<<">"<<c<<">"<<a<<endl;   
  25.     }
  26.     else if(c>a&&a>b)                   //cab
  27.     {
  28.        cout<<c<<">"<<a<<">"<<b<<endl;   
  29.     }
  30.     else if(c>b&&b>a)                   //cba
  31.     {
  32.        cout<<c<<">"<<b<<">"<<a<<endl;   
  33.     }
  34.     else
  35.     {
  36.        cout<<"其中兩個或三個數相等"<<endl;
  37.     }
  38.     goto re;
  39.     system("pause");
  40.     return 0;
  41. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    int a,b,c,max;
  7.    cout<<"請依序輸入三個數字並按enter:";
  8.   cin>>a>>b>>c;
  9.     if(a>c && c>b)
  10.     {
  11.         cout<<a<<">"<<b<<">"<<c<<endl;
  12.     }else if(a>b && b>c)
  13.     {
  14.         cout<<c<<">"<<b<<">"<<b<<endl;
  15.     }else if(b>c && c>a)
  16.     {
  17.         cout<<b<<">"<<c<<">"<<a<<endl;
  18.          }else if(c>b && b>a)
  19.     {
  20.         cout<<c<<">"<<b<<">"<<a<<endl;
  21.         }  
  22.         else if(c>a && a>b)
  23.     {
  24.         cout<<c<<">"<<b<<">"<<a<<endl;
  25.         } else
  26.         cout <<"其中兩個數, 或三個數相等!"<<endl;
  27.         
  28.         
  29.         
  30.     system("pause");
  31.     return 0;
  32. }
複製代碼

TOP

返回列表