Board logo

標題: 求最大公因數 (輾轉相除法) [打印本頁]

作者: 方浩葦    時間: 2024-5-1 13:29     標題: 求最大公因數 (輾轉相除法)



試以輾轉相除法,解最大公因數。

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

作者: 鄭豊翰    時間: 2024-5-11 16:25

本帖最後由 鄭豊翰 於 2024-5-11 16:30 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {  
  6.     re:
  7.     int x, y, op;
  8.     cout<<"請依順序輸入兩個正整數: ";
  9.     cin>>x>>y;
  10.     cout<<x<<"與"<<y<<"的最大公因數:";
  11.         while(x%y!=0)
  12.     {
  13.         op=x%y;
  14.         x=y;
  15.         y=op;
  16.     }
  17.     cout<<y<<endl;   
  18.         goto re;
  19.     return 0;   
  20. }
複製代碼





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