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

TOP

返回列表