最大公约数和最小公倍数

简介:
#include <stdio.h>


// 功能描述:求两个数的最大公约数
long gcd(int lhs, int rhs)
{
	int r = 0;

	while (rhs)
	{
		r = lhs % rhs;
		lhs = rhs;
		rhs = r;
	}

	return lhs;
}

//功能描述:求两个数的最小公倍数
long lcd(int lhs, int rhs)
{
	long g = gcd(lhs, rhs); // 计算两个数的最大公约数

	return lhs * rhs / g; // 计算两个数的最小公倍数
}

int main()
{
	int lhs, rhs;
	scanf("%d%d", &lhs, &rhs);
	printf("最大公约数:%d\n", gcd(lhs, rhs));
	printf("最小公倍数:%d\n", lcd(lhs, rhs));

	return 0;
}

目录
相关文章
|
1天前
最大公约数和最小公倍数
最大公约数和最小公倍数
10 4
|
2月前
|
算法
详解最大公约数和最小公倍数
详解最大公约数和最小公倍数
|
9月前
求最小公倍数
求最小公倍数
48 0
|
8月前
wustojc5002最大公约数
wustojc5002最大公约数
27 0
|
9月前
求最大公约数
求最大公约数
37 0
|
9月前
1207:求最大公约数问题
1207:求最大公约数问题
|
10月前
|
人工智能 BI
求最大公约数和最小公倍数
求最大公约数和最小公倍数
53 0