閱讀715 返回首頁    go 汽車大全


最大公約數和最小公倍數

#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;
}

最後更新:2017-04-03 18:52:02

  上一篇:go 穀歌搜索成功秘訣:以簡潔贏得用戶
  下一篇:go 第六章 Hibernate jar包