两个数字的GCD是将两个数字相除的最大数字。查找GCD的一种简单方法是分解两个数并乘以公因数。
// C program to demonstrate working of extended
// Euclidean Algorithm
#include
// C function for extended Euclidean Algorithm
int gcdExtended(int a, int b, int* x, int* y)
{
// Base Case
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
int x1, y1; // To store results of recursive call
int gcd = gcdExtended(b % a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
// Driver Program
int main()
{
int x, y;
int a = 35, b = 15;
int g = gcdExtended(a, b, &x, &y);
printf("gcd(%d, %d) = %d", a, b, g);
return 0;
}
输出:
gcd(35, 15) = 5
请参阅有关基本和扩展欧几里德算法的完整文章,以了解更多详细信息!
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。