返回列表 發帖
本帖最後由 林少謙 於 2024-7-6 15:05 編輯
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y,smaller,a;
  7.     cout<<"請輸入第一個正整數:";
  8.     cin>>x;
  9.     cout<<"請輸入第二個正整數:";
  10.     cin>>y;
  11.     smaller=x<y?x:y;
  12.     cout<<x<<"和"<<y<<"的"<<"的公因數有:";
  13.     for (int i=1 ; i<=smaller ; i++)
  14.     {
  15.         if (x%i==0&&y%i==0)
  16.         {
  17.             cout<<i<<" ";
  18.             a++;
  19.         }
  20.     }
  21.     cout<<endl;
  22.     cout<<"共有"<<a<<"個"<<endl;
  23.     cout<<endl;
  24.     system("pause");
  25.     return 0;
  26. }
複製代碼

TOP

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y,z,a,b,sum;
  7.     cout<<"請輸入第一個正整數:";
  8.     cin>>x;
  9.     cout<<"請輸入第二個正整數:";
  10.     cin>>y;
  11.     cout<<"請輸入第三個正整數:";
  12.     cin>>z;
  13.     a=x<y?x:y;
  14.     b=a<z?a:z;
  15.     cout<<x<<"和"<<y<<"和"<<z<<"的"<<"的公因數有:";
  16.     for (int i=1 ; i<=b ; i++)
  17.     {
  18.         if (x%i==0&&y%i==0&&z%i==0)
  19.         {
  20.             cout<<i<<" ";
  21.             sum++;
  22.         }
  23.     }
  24.     cout<<endl;
  25.     cout<<"共有"<<sum<<"個"<<endl;
  26.     cout<<endl;
  27.     system("pause");
  28.     return 0;
  29. }
複製代碼

TOP

返回列表