返回列表 發帖

因數分解 - 二數求公因數

本帖最後由 tonyh 於 2011-11-26 17:25 編輯

讓使用者任意輸入兩正整數, 電腦回應它們的公因數有那些.
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int a, b, min;
  6.     cout<<"請輸入第一個數: ";
  7.     cin>>a;
  8.     cout<<"請輸入第二個數: ";
  9.     cin>>b;
  10.     min=(a<b)?a:b;
  11.     cout<<a<<"與"<<b<<"的公因數有: ";
  12.     for(int i=1; i<=min; i++)
  13.     {
  14.          if(a%i==0 && b%i==0)
  15.          {
  16.                cout<<i<<" ";            
  17.          }  
  18.     }
  19.     cout<<endl;
  20.     system("pause");
  21.     return 0;
  22. }
複製代碼

返回列表