返回列表 發帖

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

試設計一個小程式, 讓使用者任意輸入三個數, 接著電腦回應出這三個數的大小順序, 由大而小排列出.
譬如:
當使用者依序輸入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.     int a, b, c;
  7.     re:
  8.     cout<<"請任意輸入三個數: ";
  9.     cin>>a>>b>>c;
  10.     if(a>b && b>c)
  11.         cout<<a<<">"<<b<<">"<<c<<endl;
  12.     else if(a>c && c>b)
  13.         cout<<a<<">"<<c<<">"<<b<<endl;
  14.     else if(b>a && a>c)
  15.         cout<<b<<">"<<a<<">"<<c<<endl;
  16.     else if(b>c && c>a)
  17.         cout<<b<<">"<<c<<">"<<a<<endl;
  18.     else if(c>a && a>b)
  19.         cout<<c<<">"<<a<<">"<<b<<endl;
  20.     else if(c>b && b>a)
  21.         cout<<c<<">"<<b<<">"<<a<<endl;
  22.     else
  23.         cout<<"其中兩個數, 或三個數相等!"<<endl;
  24.     cout<<endl;
  25.     goto re;
  26.     system("pause");
  27.     return 0;
  28. }
複製代碼

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5.     re:
  6.     int a,b,c;
  7.     cout<<"請依序輸入三個數:"<<endl;
  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.     goto re;

  24.     system("pause");
  25.     return 0;
  26. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5. int x,y,z;
  6. re:
  7. cout<<"請依序輸入任意三數";
  8. cin>>x>>y>>z;
  9. if(x>y&&y>z)
  10.     cout<<x<<">"<<y<<">"<<z<<endl;
  11. else if (x>z&&z>y)
  12.     cout<<x<<">"<<z<<">"<<y<<endl;
  13. else if (y>x&&x>z)
  14.     cout<<y<<">"<<x<<">"<<z<<endl;
  15. else if (y>z&&z>x)
  16.     cout<<y<<">"<<z<<">"<<x<<endl;
  17. else if (z>x&&x>y)
  18.     cout<<z<<">"<<x<<">"<<y<<endl;
  19. else if (z>y&&y>x)
  20.     cout<<z<<">"<<y<<">"<<x<<endl;
  21. else
  22.     cout<<"其中兩數或三數相等"<<endl;
  23. cout<<endl;








  24. goto re;

  25. system("pause");
  26. return 0;

  27. }
複製代碼

TOP

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

TOP

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

TOP

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

TOP

返回列表