算法设计——计算两个数的最大公约数

版权声明:版权所有,违权必究!!! https://blog.csdn.net/IT_SoftEngineer/article/details/82872349

函数实现

// greatest common divisor (最大公约数)
int GCD (int a, int b)
{
    if(b==0)
        return a;
    else
        return GCD(b,a%b);
}

函数用途

可用来化简分数为最简分数

 代码实现

#include<iostream>
using namespace std;
// 求最大公约数的函数说明
int GCD(int a, int b)
{
	if (b == 0)
		return a;
	else
		return GCD(b, a%b);
}
int main()
{
	int m, n, divisor;
	cin >> m >> n;
	divisor = GCD(m, n);
	cout << m << "/" << n << "=" << m / divisor << "/" << n/divisor << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/IT_SoftEngineer/article/details/82872349