返回列表 發帖

因數分解 (六) - 求最大公因數 (break敘述)

利用break敘述,於符合條件時,立即跳出迴圈。



本帖隱藏的內容需要回復才可以瀏覽

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y,smaller,tmp;
  7.         cout<<"請依序輸入兩數";
  8.         cin>>x>>y;
  9.         cout<<x<<"和"<<y<<"的最大公因數為:";
  10.         smaller=x<y?x:y;
  11.         for(int i=smaller;i>=1;i--)
  12.         {
  13.         if(x%i==0&&y%i==0)
  14.           tmp=i;
  15.         break;
  16.         }
  17.        
  18.         cout<<tmp<<endl;

  19.         system("pause");
  20.         return 0;

  21. }
複製代碼

TOP

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

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5.     re:
  6.     int a,b,s,tmp;
  7.     cout<<"請輸入第一個正整數:"<<endl;
  8.     cin>>a;
  9.     cout<<"請輸入第二個正整數:"<<endl;
  10.     cin>>b;
  11.     cout<<endl;
  12.     cout<<a<<"和"<<b<<"的最大公因數=";
  13.     s=a<b?a:b;
  14.     for(int i=s;i>=1;i--){
  15.         if(a%i==0 && b%i==0){
  16.            tmp=i;
  17.            break;
  18.         }
  19.     }cout<<tmp<<endl;
  20. goto re;
  21. system("pause");
  22. return 0;
  23. }
複製代碼

TOP

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

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     re:
  7.     int a, b, c, d;
  8.     cout<<"請輸入第一個數: ";
  9.     cin>>a;
  10.     cout<<"請輸入第二個數: ";
  11.     cin>>b;
  12.     c=a<b?a:b;
  13.     cout<<a<<"與"<<b<<"的最大公因數為: ";
  14.     for(int i=c; i<=c; i--)
  15.     {
  16.         if(a%i==0 && b%i==0)
  17.         {
  18.             d=i;
  19.             break;
  20.         }
  21.     }
  22.     cout<<d<<endl;
  23.     goto re;
  24.     return 0;
  25. }
複製代碼

TOP

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

TOP

本帖最後由 高湘庭 於 2024-7-6 15:41 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5.     int x,y,smaller,tmp;
  6. cout<<"請依序輸入兩數";
  7. cin>>x>>y;
  8. cout<<x<<"和"<<y<<"的最大公因數為:";
  9. smaller=x<y?x:y;
  10. for(int i=smaller;i>=1;i--){
  11. if(x%i==0&&y%i==0)
  12.   tmp=i;
  13. break;//***一找到最大公因數就跳出迴圈(由大->小)

  14. }
  15. cout<<tmp<<endl;

  16. system("pause");
  17. return 0;

  18. }
複製代碼

TOP

返回列表