返回列表 發帖
本帖最後由 董宸佑 於 2019-11-8 20:56 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {   
  6.     int m, n, s, gcd;
  7.     cout<<"請輸入第一個正整數: ";
  8.     cin>>m;
  9.     cout<<"請輸入第二個正整數: ";
  10.     cin>>n;
  11.     s=m<n?m:n;
  12.     cout<<m<<"與"<<n<<"的最大公因數是: ";
  13.     for(int i=s; i>=1; i--)
  14.     {
  15.         if(m%i==0 && n%i==0)
  16.         {
  17.             gcd=i;
  18.             break;         
  19.         }      
  20.     }
  21.     cout<<gcd<<endl<<endl;
  22.     system("pause");
  23.     return 0;   
  24. }
複製代碼

TOP

返回列表