C语言最大公约数和最小公倍数算法实现
#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; }