返回列表 發帖

三數求公因數

讓使用者依序輸入三個正整數, 電腦回應它們有那些公因數, 以及共有幾個公因數.
執行畫面如下:



本帖隱藏的內容需要回復才可以瀏覽

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a, b, c, d, n=0;
  7.     cout<<"請依序輸入三個正整數: ";
  8.     cin>>a>>b>>c;
  9.     d=a<b?a:b;
  10.     d=d<c?d:c;
  11.     cout<<a<<","<<b<<"與"<<c<<"的公因數有: ";
  12.     for(int i=1;i<=d;i++)
  13.     {
  14.         if(a%i==0 && b%i==0 && c%i==0)
  15.         {
  16.             cout<<i<<" ";
  17.             n++;
  18.         }
  19.     }
  20.     cout<<endl<<"總共有"<<n<<"個!"<<endl;
  21.     system("pause");
  22.     return 0;
  23. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.         int a, b, c, d, n=0;
  7.     cout<<"請依序輸入三個正整數: ";
  8.     cin>>a>>b>>c;
  9.     cout<<a<<","<<b<<"與"<<c<<"的公因數有: ";
  10.     for(int i=1;i<=a;i++)
  11.     {
  12.         if(a%i==0 && b%i==0 && c%i==0)
  13.         {
  14.             cout<<i<<" ";
  15.             n++;
  16.         }
  17.     }
  18.     cout<<endl<<"總共有"<<n<<"個!"<<endl;
  19.     system("pause")        ;
  20.         return 0;
  21. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a,b,c,n=0;
  7.     cout<<"請依序輸入三個正整數: ";
  8.     cin>>a>>b>>c;
  9.     cout<<a<<","<<b<<"與"<<c<<"的公因數有: ";
  10.     for(int i = 1;i <= a;i++)
  11.     {
  12.         if(a%i==0&&b%i==0&&c%i==0)        
  13.         {
  14.             cout<<i<<" ";                                 
  15.             n++;
  16.         }
  17.     }  
  18.     cout<<"\n總共有"<<n<<"個\n";
  19.     system("pause");
  20.     return 0;   
  21. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a,b,c,s=0;
  7.     cout << "依序輸入三個正整數: ";
  8.     cin >> a >> b >> c;
  9.     cout << a << "," << b << "與" << c << "的公因數有: \n";
  10.     for (int i=1;i<=a;i++){
  11.         if (a%i==0 && b%i==0 && c%i==0){
  12.             cout << i << " ";
  13.             s++;
  14.         }
  15.     }
  16.     cout << "\n總共有" << s << "個!\n";
  17.     return 0;
  18. }
複製代碼

TOP

返回列表