返回列表 發帖

二數求最小公倍數 (break敘述)

本帖最後由 tonyh 於 2015-7-24 21:02 編輯

讓使用者任意輸入兩個正整數, 求它們的最小公倍數.
提示: 加入break敘述, 使符合條件時, 跳出迴圈.



本帖隱藏的內容需要回復才可以瀏覽
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     re:
  7.     int x, y, b;
  8.     cout<<"第一個數: ";
  9.     cin>>x;
  10.     cout<<"第二個數: ";
  11.     cin>>y;
  12.     b=x>y?x:y;      
  13.     cout<<x<<"與"<<y<<"最小公倍數: ";
  14.     for(int i=b; i<=x*y; i++)
  15.     {
  16.          if(i%x==0 && i%y==0)
  17.          {
  18.              cout<<i<<endl;
  19.              break;
  20.          }  
  21.     }
  22.     goto re;   
  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 x, y, big;
  8.     cout<<"請輸入第一個數: ";
  9.     cin>>x;
  10.     cout<<"請輸入第二個數: ";
  11.     cin>>y;
  12.     bigger=x>y?x:y;      
  13.     cout<<x<<"與"<<y<<"的最小公倍數是: ";
  14.     for(int i=big; i<=x*y; i++)
  15.     {
  16.          if(i%x==0 && i%y==0)
  17.          {
  18.              cout<<i<<endl;
  19.              break;
  20.          }  
  21.     }
  22.     goto re;   
  23.     system("pause");
  24.     return 0;
  25. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace  std;
  4. int main(){
  5.     re:
  6.     int x, y;
  7.     cout<<"請輸入第一個數: ";
  8.     cin>>x;
  9.     cout<<"請輸入第二個數: ";
  10.     cin>>y;
  11.     cout<<x<<"和"<<y<<"的最小公倍數為:";
  12.     for(int i=x>y?x:y; i<=x*y; i++){
  13.         if(i%x==0 && i%y==0){
  14.            cout<<i<<endl;
  15.            break;
  16.         }
  17.     }
  18.     goto re;
  19.     system("pause");
  20.     return 0;
  21. }
複製代碼
ABCDEFGHIJKLMNOPQRSTUVWXYZ

TOP

本帖最後由 林侑成 於 2015-7-24 21:05 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     re:
  7.     int x, y, bigger;
  8.     cout<<"請輸入第一個數: ";
  9.     cin>>x;
  10.     cout<<"請輸入第二個數: ";
  11.     cin>>y;
  12.     bigger=x>y?x:y;      
  13.     cout<<x<<"與"<<y<<"的最小公倍數是: ";
  14.     for(int i=bigger; i<=x*y; i++)
  15.     {
  16.          if(i%x==0 && i%y==0)
  17.          {
  18.              cout<<i<<endl;
  19.              break;
  20.          }  
  21.     }
  22.     goto re;   
  23.     system("pause");
  24.     return 0;
  25. }
複製代碼
我是眾神之王XXX  I love you
0000000000

TOP

返回列表