本帖最後由 黃辰昊 於 2019-11-8 20:55 編輯
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int a, b, smaller, gcd;
- re:
- cout<<"請輸入第一個數: ";
- cin>>a;
- cout<<"請輸入第二個數: ";
- cin>>b;
- cout<<"最大公因數= ";
- smaller=a<b?a:b;
- for(int i=smaller; i>=1; i--)
- {
- if(a%i==0 && b%i==0)
- {
- gcd=i;
- break;
- }
- }
- cout<<gcd<<endl;
- goto re;
- system("pause");
- return 0;
- }
複製代碼 |