C++ 請大大幫忙 遇到困難 幫忙寫出這題的程式 第1題:請製作使用歐基里德算法來求得整數值x與y的最大公約數的函數 int gcd(int x, int y);?

2015-12-30 2:56 pm

回答 (2)

2015-12-31 1:26 am
unsigned gcd(unsigned a,unsigned b){
unsigned x[2]={a,b},i;
for(i=x[0]>x[1];x[i];i=!i){x[!i]%=x[i];}
return x[!i];
}
2015-12-30 6:10 pm
Try the following, improve if necessary.
int gcd(int x, int y){
int X,Y,tmp;
if(x==y)return x;
if(x<y){X=x;Y=y;}
else {X=y; Y=x;}
while(X>=1){
t=Y; Y=X; X=t-t/X*X;
}
return Y
}


收錄日期: 2021-04-18 14:17:07
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20151230065646AA2a03M

檢視 Wayback Machine 備份