作者johnjohnlin (嗯?)
看板EE_DSnP
标题[请益] gcd 的问题
时间Thu Nov 6 20:06:30 2008
先附程式码
int main(){
cout<<"enter two intergers"<<endl;
int a ; int b;
cin>>a;
cin>>b;
cout<< "the GCD of "<<a<<" and "<<b <<"= "<<
gcd(a,b)<<endl;
system("pause");
}
int gcd(int x,int y){
if(x>y){int c ; c=x;x=y; y=c;}
else{}
if(y%x==0){
cout<< "the GCD is "<<x <<endl; return x;}
else{gcd(y%x,x);}
}
输出结果
1.正常情形
enter two intergers
13
169
the GCD is 13
the GCD of 13 and 169= 13
请按任意键继续 . . .
2.不知名的bug
enter two intergers
12
8
the GCD is 4
the GCD of 12 and 8= 2088809675
请按任意键继续 . . .
请问 为什麽 一个是 return x; 再cout
一个是直接cout x
输出的会不一样?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.241.150
1F:→ ilway25:没有问题耶.. 11/06 20:24
2F:推 ric2k1:我想你的 else return gcd(...) 比较好. 11/06 20:52
3F:推 stimim:我比较好奇的是,gcd(0,0)会发生甚麽事? 11/06 22:46
4F:推 ric2k1:关於 gcd 可以参考 #584 #585 11/06 22:53
5F:→ johnjohnlin:还是不行...好像把int 改成 void 不加 return 就行了 11/07 00:00
6F:推 ric2k1:不会吧, 我是说改成: else{ return gcd(y%x,x); } 11/07 00:25
7F:→ ric2k1:应该就可以了... 11/07 00:25
8F:→ ric2k1:不过你 gcd(0,0) 会 crash 哦! 11/07 00:26