Board logo

標題: 因數分解 (六) - 求最大公因數 (break敘述) [打印本頁]

作者: 方浩葦    時間: 2024-5-1 13:29     標題: 因數分解 (六) - 求最大公因數 (break敘述)

利用break敘述,於符合條件時,立即跳出迴圈。



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

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

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





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