#include<iostream>
using namespace std;
void PaiXu(int &m, int &n) //排序,将大的数放在最前面,方便great函数中两个数相除求余数a
{
int swop;
if (m < n)
{
swop = m;
m = n;
n = swop;
}
}
int great(int m, int n)
{
int a;
PaiXu(m, n);
a = m%n; //先计算两个数相除的余数
while (a) //while循环意思是拿最小的数和余数相除,直到余数为零为止,那么未计算余数之前的a就是最大公约数
{
m = n;
n = a;
a = m%n;
}
return n; //此处返回的n就是最大公约数,因为在while循环中{ n=a }
}
int main()
{
int m, n, a,b; //定义a为最大公约数,b为最小公倍数
cout << "input two numbers a and b: ";
cin >> m >> n;
a = great(m, n);
cout << m << "and " << n<<"greatest common divisor" << a << endl;
b = m*n / a; //最小公倍数的计算方法:两个数之积除最大公约数
cout << m << "and" << n << " lowest common multiple" << b << endl;
system("pause");
return 0;
}