查看: 1212|回复: 5
|
I dont understand this question.
[复制链接]
|
|
Given positive two integers m and n such that m<n, the greatest common divisor of m and n is the same as the greatest common divisor of m and (n-m). Use this fact to write a recursive definition of the function "greatest_common_divisor(...)", which takes two positive integer arguments and returns their greatest common divisor. Test your function in a suitable main program.
Can anybody explain what is this question about?
got any example?
请使用中文发表!
[ 本帖最后由 白日梦 于 22-12-2005 09:20 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 22-12-2005 03:20 PM
|
显示全部楼层
是要你找出最大公约数(GCD)。。。如果m=12, n=36。那m 和 n 都能被1, 2, 3, 4, 6, 12 整除。 其中以12最大, 那 bcd = 12。
可以参考欧几里德算法或Stein算法. |
|
|
|
|
|
|
|
发表于 22-12-2005 03:46 PM
|
显示全部楼层
tutorial 的问题也拿来这里问? 我的老天..还一字不漏的写出来.... -_-||| |
|
|
|
|
|
|
|
发表于 25-12-2005 09:59 AM
|
显示全部楼层
问题应该是这样:
(1)有两个号码 m 和 n .......
m < n
(2)p= (n-m);
GCD(m,n) = GCD(m,p)
(3)根据(2),写出一个RECURSIVE FUNCTION GCD(m,n)。
答案应该是:
GCD(m,n)
{
if(m>n)
{ return GCD(n,m) }
else
{
if((n mod m) == 0)
{ return m }
else
{ return GCD(m,n-m) }
}
}
如 GCD(105,45) = GCD(45,105)
= GCD(45,60)
= GCD(45,15)
= GCD(15,45)
= 15
[ 本帖最后由 kee020041 于 25-12-2005 10:02 AM 编辑 ] |
|
|
|
|
|
|
|
发表于 25-12-2005 05:46 PM
|
显示全部楼层
需不需要考虑到如果两者都不能整除,例如(7,22) |
|
|
|
|
|
|
|
发表于 25-12-2005 09:36 PM
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|