本帖最後由 許婷芳 於 2019-11-8 20:39 編輯
提示:
x y
35 / 56 = 0 ... 35
56 / 35 = 1 ... 21
35 / 21 = 1 ... 14
21 / 14 = 1... 7
14 / 7 = 2 ... 0
試以輾轉相除法,解最大公因數。- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- re:
- int x,y,tmp;
- cout<<"請依序輸入兩個正整數: ";
- cin>>x>>y;
- cout<<x<<"與"<<y<<"的最大公因數為: ";
- while(x%y!=0)
- {
- tmp=x%y;
- x=y;
- y=tmp;
- }
- cout<<y<<endl<<endl;
- goto re;
- system("pause");
- return 0;
- }
複製代碼 |