作者ric2k1 (Ric)
看板EE_DSnP
標題Re: 求最大公因數遞迴
時間Wed Oct 10 00:56:04 2007
剛剛沒看到要寫成遞回, 其實遞迴的程式比較不容易看懂, 除非必要,
否則還是用 while 就好...
#include<iostream>
using namespace std;
int GCD(int a,int b)
{
if (b != 0)
return GCD(b, a%b);
return a;
}
int main()
{
int a,b,gcd; // 求出gcd=(a,b)
cout<<"求最大公因數"<<endl;
cout<<"請輸入兩個整數"<<endl;
cin>>a>>b;
gcd=GCD(a,b);
cout<<"(a,b)="<<gcd<<endl;
system("pause");
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.121.131.190
1F:→ lee7938:這個是作業嗎???好像沒有看到。還是只是課本上的習題? 10/10 20:39
2F:→ ric2k1:這些要到 chap 4, 5, 6, 7, 8... 才會教 10/11 23:13