返回列表 發帖

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

本帖最後由 tonyh 於 2014-12-20 11:47 編輯

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

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x, y, bigger, minf;
  7.     cout<<"請輸入第一個數: ";
  8.     cin>>x;
  9.     cout<<"請輸入第二個數: ";
  10.     cin>>y;
  11.     bigger=(x>y)?x:y;
  12.     for(int i=bigger; i<=x*y; i++)
  13.     {
  14.          if(i%x==0 && i%y==0)
  15.          {
  16.               minf=i;
  17.               break;     
  18.          }
  19.     }
  20.     cout<<x<<"與"<<y<<"的最小公倍數為: "<<minf<<endl;
  21.     system("pause");
  22.     return 0;
  23. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    int x,y,sm,m;
  7.    cout<<"請輸第一個數";
  8.    cin>>x;
  9.    cout<<"請輸第二個數";
  10.    cin>>y;
  11.    sm=(x>y)?x:y;

  12.       
  13.    for( int i=sm;i<=x*y ;i++)
  14.    {
  15.       
  16.        if(i%x==0 && i%y==0)
  17.        {         
  18.            
  19.            
  20.            m=i;
  21.            break;
  22.        }
  23.    }
  24.    
  25.    cout<<x<<"與"<<y<<"的最小公倍數是:"<<m<<endl;
  26.    system("pause");
  27.    return 0;
  28. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y,a,b;
  7.     cout<<"請輸入兩個數"<<endl;
  8.     cin>>x;
  9.     cin>>y;
  10.     a=(x<y)?x:y;
  11.     for(int i=a ;i<=x*y;i++)
  12.     {
  13.          if(i%x==0 && i%y==0)
  14.          {
  15.               b=i;
  16.               break;
  17.          }
  18.     }
  19.     cout<<b;
  20.     system("pause");
  21.     return 0;

  22. }
複製代碼
http://fs-old.mis.kuas.edu.tw/~s1102137106/music/

TOP

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

TOP

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

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a,b,big,least;
  7.     cout<<"請輸入第一個數:";
  8.     cin>>a;
  9.     cout<<"請輸入第二個數:";
  10.     cin>>b;
  11.     big=(a<b)?a:b;
  12.    
  13.     int i=big;
  14.     while(i<=a*b)
  15.     {
  16.         if(i%a==0 and i%b==0)
  17.         {
  18.             least=i;
  19.             break;
  20.         }
  21.         i++;
  22.         
  23.     }
  24.     cout<<a<<"與"<<b<<"的最小公倍數為"<<least<<endl;
  25.    
  26.     system("pause");
  27.     return 0;
  28. }
複製代碼

TOP

返回列表