本帖最後由 楊炘樺 於 2025-1-17 17:24 編輯
- #include<iostream>
- using namespace std;
- int main()
- {
- re:
- int n1,n2,small,sum;
- cout<<"請輸入第一個數: ";
- cin>>n1;
- cout<<"請輸入第二個數: ";
- cin>>n2;
- if(n1<=0 || n2<=0 || n1==n2)
- {
- cout<<"輸入錯誤!\n\n";
- goto re;
- }
- cout<<n1<<"與"<<n2<<"的最大公因數是: ";
- small=(n1<n2)?n1:n2;
- for(int i=small;i>=1;i--)
- {
- if(n1%i==0 && n2%i==0)
- {
- sum=i;
- break;
- }
- }
- cout<<sum<<"\n\n";
- goto re;
- return 0;
- }
複製代碼 |