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

TOP

返回列表