作者popo4231 (小泰)
看板EE_DSnP
標題求最大公因數遞迴
時間Tue Oct 9 23:45:49 2007
#include<iostream>
using namespace std;
void swap(int* a,int* b)//若a<b則對調a與b
{
int temp;
if(*a<*b)
{
temp=*a;
*a=*b;
*b=temp;
}
}
int GCD(int a,int b)
{
int r;//貯存餘數
r=a%b;
if(r==0)
{
return b;
}
else
{
cout<<r<<endl;//印出計算過程
for(int i=1;i<=100000000;i++);//delay空迴圈
GCD(b,r);
}
}
int main()
{
int a,b,gcd;//求出gcd=(a,b)
cout<<"求最大公因數"<<endl;
cout<<"請輸入兩個整數"<<endl;
cin>>a>>b;
swap(&a,&b);
gcd=GCD(a,b);
cout<<"(a,b)="<<gcd<<endl;
system("pause");
return 0;
}
只要不能整除的都會出錯
請問哪邊錯了
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.241.177
1F:推 timrau:因為沒有return GCD(b,r) 10/09 23:55
2F:推 ric2k1:嗯, please pay attention to the compilation warning... 10/10 00:05
3F:→ ric2k1:main6.cpp:27: warning: control reaches end of non-void 10/10 00:05
4F:→ ric2k1:function 10/10 00:06
5F:→ ric2k1:("main6.cpp" is the filename I used to test...) 10/10 00:06
6F:推 ijb:這不會是老師給同學的加分題吧...結果被饒神秒殺 10/10 10:29