返回列表 發帖
本帖最後由 t3742238 於 2011-11-27 10:02 編輯
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int a, b, c,d, e=0,min;
  6.     cout<<"請輸入第一個數: ";
  7.     cin>>a;
  8.     cout<<"請輸入第二個數: ";
  9.     cin>>b;
  10.     cout<<"請輸入第三個數: ";
  11.     cin>>c;   
  12.     d=(a<b)?a:b;
  13.     min=(c<d)?c:d;
  14.     cout<<a<<"與"<<b<<"與"<<c<<"的公因數有: ";
  15.     for(int i=1; i<=min; i++)
  16.     {
  17.          if(a%i==0 && b%i==0 &&b%i==0)
  18.          {
  19.                cout<<i<<" "<<endl;
  20.                e++;            
  21.          }  
  22.     }
  23.     cout<<"總共有"<<e<<"個公因數"<<endl;
  24.     cout<<endl;
  25.     system("pause");
  26.     return 0;
  27. }
複製代碼

TOP

  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. }
複製代碼

TOP

返回列表