求最大公约数的欧几里德算法,也称作辗转相除法,如下:
int GetGCD(int m, int n)
{
if (n = 0)
{
return m;
}
return GetGCD(n, m % n);
}
(GCD = Greatest Common Divisor)
比如
GetGCD(18, 24)
-> GetGCD(24, 18)
-> GetGCD(18, 6);
-> GetGCD(6, 0);
-> return 6;
----------------------------------------
另外,最小公倍数 = 两数之积 / 最大公约数
所以 GetLCM(m, n) = m * n / GetGCD(m, n)
(LCM = Least Common Multiple)
比如
GetLCM(18, 24) = 18 * 24 / 6 = 72
网络资源的拷贝粘贴 备份参考之用
30 January 2011
Subscribe to:
Post Comments (Atom)
-
{
New Yorker
(11)
}
{
fiction
(11)
}
{
Programming
(9)
}
{
people
(7)
}
{
Google
(6)
}
{
algorithm
(5)
}
{
Internet
(4)
}
{
MIT Media Lab
(4)
}
{
photo
(4)
}
{
picture
(4)
}
{
United States presidential election 2008
(3)
}
{
book
(3)
}
{
冯象
(3)
}
{
电影
(3)
}
{
Erik Demaine
(2)
}
{
Information Retrieval
(2)
}
{
Neil Gaiman
(2)
}
{
Power Law
(2)
}
{
Steve_Jobs
(2)
}
{
movie
(2)
}
{
典故
(2)
}
{
思维的乐趣
(2)
}
{
Art
(1)
}
{
Auto Summarization
(1)
}
{
Barack Hussein Obama
(1)
}
{
Classical Music
(1)
}
{
French
(1)
}
{
Gallery
(1)
}
{
Google Video
(1)
}
{
HARUKI MURAKAMI
(1)
}
{
Hilary Clinton
(1)
}
{
Javascript
(1)
}
{
Psychology
(1)
}
{
cheat sheet
(1)
}
{
data structure
(1)
}
{
foosball
(1)
}
{
lyrics
(1)
}
{
movie making
(1)
}
{
music
(1)
}
{
poem
(1)
}
{
ranking
(1)
}
{
wikipedia
(1)
}
{
三联生活周刊
(1)
}
{
拍电影
(1)
}
{
数学之美系列
(1)
}
{
村上春树
(1)
}
{
算法
(1)
}
{
美术馆
(1)
}
No comments:
Post a Comment