作者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