返回列表 發帖

[作業] 因數分解 - 三數求公因數

本帖最後由 tonyh 於 2011-12-3 16:12 編輯

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

返回列表