Board logo

標題: [隨堂測驗] 因數分解 (五) - 求最大公因數 [打印本頁]

作者: 方浩葦    時間: 2024-4-19 11:34     標題: [隨堂測驗] 因數分解 (五) - 求最大公因數

讓使用者任意輸入兩個正整數, 求它們的最大公因數.



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

作者: 鄭豊翰    時間: 2024-5-11 15:58

本帖最後由 鄭豊翰 於 2024-5-11 16:02 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {  
  6.     re:
  7.     int x, y, smaller, op;
  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<=x; i++)
  15.     {
  16.         if(x%i==0 && y%i==0)
  17.         {   
  18.             op=i;
  19.         }
  20.     }
  21.     cout<<op<<endl;   
  22.         goto re;
  23.     return 0;   
  24. }
複製代碼





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