/* the algorithm of Euclid */
#include <iostream>
int gcd(int,int);//the function of Greatest Common Divisor
/*****************************************************
*example:
* gcd(18,12) = gcd(12,18 mod 12) = gcd(12,6) =
* gcd(6,12 mod 6) = gcd(6,0) = 6
*****************************************************/
int gcd (int a,int b)
{
a = a%b;
if(!a)
return b;
else
return gcd(b,a);
}
int main(int argc,char** argv)
{
int a = 18,b = 12;
std::cout<<gcd(a,b)<<std::endl;
return 0;
}
本文介绍了一个使用C++实现的欧几里得算法来计算两个整数的最大公约数(GCD)。通过递归的方式实现了该算法,并给出了具体的示例代码,展示了如何求出18和12的最大公约数。
1059

被折叠的 条评论
为什么被折叠?



