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

TOP

返回列表