我正在尝试制作一个模型类来找到 2 个数字的格栅共同除法。如果我输入 68 和 22 答案是 2 并且是正确的,但如果我输入 678 352 结果是 326 并且是错误的!应该是2!有任何想法吗??
def gcd
if firstnumber > secondnumber
div= firstnumber/ secondnumber
ip=firstnumber % secondnumber
fi = div / ip
ip
else
i = firstnumber
s = secondnumber
secondnumber = firstnumber
firstnumber = s
div= firstnumber/ secondnumber
ip=firstnumber % secondnumber
fi = div / ip ip
end
end
带循环
def gcd
a = firstnumber
b=secondnumber
ip = 50
if firstnumber > secondnumber
begin
fi=a/b
re=a%b
fi=b/re
re=re%b
ip=re
end until ip != 0
return re
end
else
i = firstnumber
s = secondnumber
secondnumber = firstnumber
firstnumber = s
div= firstnumber/ secondnumber
ip=firstnumber % secondnumber
fi = div / ip
ip
end
end
$结束
同样的事情也会发生