#include <iostream>
#include <cstdio>
#define MAX(a,b) (a > b ? a : b)
template <class T>
T gcd(T a, T b)
{
return a > b ? (b == 0 ? b : a%b) : gcd(b,a%b);
}
int main(void)
{
int a,b;
int resLcm = 0;
int resGcd = 0;
while (scanf("%d%d",&a,&b) != EOF)
{
resGcd = gcd(a,b); //greatest common divisor
resLcm = resGcd*MAX(a,b); //lowest common multiple
printf("resGcd = %d\nresLcm= %d\n",resGcd,resLcm);
}
return 0;
}
上面的代码利用了下C++里面的函数模版,更加通用。