返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {  
  6.     re:
  7.     int x, y, z=0;
  8.     cout<<"請輸入兩個正整數: ";
  9.     cin>>x>>y;
  10.     cout<<x<<"和"<<y<<"的公因數有: ";
  11.     if(x>y)
  12.     {
  13.         for(int i=1; i<=x; i++)
  14.         {
  15.             if(x%i==0 && y%i==0)
  16.             {   
  17.                  cout<<i<<" ";
  18.                  z++;
  19.             }
  20.         }
  21.     }else if(y>x)
  22.     {
  23.          for(int i=1; i<=y; i++)
  24.         {
  25.             if(x%i==0 && y%i==0)
  26.             {   
  27.                  cout<<i<<" ";
  28.                  z++;
  29.             }
  30.         }
  31.     }else
  32.     {
  33.          for(int i=1; i<=y; i++)
  34.         {
  35.             if(x%i==0 && y%i==0)
  36.             {   
  37.                  cout<<i<<" ";
  38.                  z++;
  39.             }
  40.         }
  41.     }
  42.     cout<<endl<<"總共有"<<z<<"個!"<<endl<<endl;
  43.     goto re;
  44.     return 0;   
  45. }
複製代碼

TOP

返回列表