Board logo

標題: 因數分解 - 二數求公因數 [打印本頁]

作者: tonyh    時間: 2019-8-31 10:44     標題: 因數分解 - 二數求公因數

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

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

作者: 蔡少宇    時間: 2019-8-31 11:14

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

作者: 郭哲維    時間: 2019-8-31 11:15

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

作者: 曾宥程    時間: 2019-8-31 11:18

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

作者: 鄭羽捷    時間: 2019-8-31 11:18

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

作者: 王翎璇    時間: 2019-8-31 11:19

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

作者: 林孟蓁    時間: 2019-8-31 11:26

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

作者: 洪榜蔓    時間: 2019-9-7 10:05

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

作者: 駱顗安    時間: 2020-7-2 17:20

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int i,j,k=1,l=0;
  7.     cout<<"請輸入第一個數:";
  8.     cin>>i;
  9.     cout<<"請輸入第二個數:";
  10.     cin>>j;
  11.     cout<<i<<"與"<<j<<"的公因數有:";
  12.     while(k<=i)
  13.     {
  14.          if(i%k==0&&j%k==0)
  15.          {
  16.                cout<<k<<" ";
  17.                l=l+1;
  18.          }
  19.          k=k+1;
  20.     }
  21.     cout<<endl;
  22.     cout<<"共有"<<l<<"個!"<<endl;
  23.     system("pause");
  24.     return 0;
  25. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2