返回列表 發帖
本帖最後由 洪藜芸 於 2019-6-15 11:18 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a,b,d=0;
  7.     cout<<"請輸入第一個數:";
  8.     cin>>a;
  9.     cout<<"請輸入第二個數:";
  10.     cin>>b;
  11.     cout<<a<<"和"<<b<<"的公因數有:";
  12.     if(a>b)
  13.     {
  14.         for(int c=1;c<=b;c++)
  15.         {
  16.             if(a%c==0 && b%c==0)
  17.             {
  18.                 cout<<c<<" ";
  19.                 d=d+1;
  20.             }
  21.             
  22.         }
  23.     }else if(a<b)
  24.     {
  25.         for(int c=1;c<=a;c++)
  26.         {
  27.             if(a%c==0 && b%c==0)
  28.             {
  29.                 cout<<c<<" ";
  30.                 d=d+1;
  31.             }
  32.         }
  33.       
  34.     }
  35.     cout<<endl;
  36.     cout<<"共有"<<d<<"個!"<<endl;
  37.     system("pause");
  38.     return 0;
  39. }
複製代碼

TOP

返回列表