习题4-7 最大公约数和最小公倍数 (15分)

#include 

void findGcdAndLcm() {
	int M, N, Gcd;
	scanf("%d %d", &M, &N);	
	Gcd = findGcd(M, N);
	printf("%d ", Gcd);
	printf("%d", findLcm(M, N, Gcd));
}

//gcd(x, y) = gcd(y, x % y) 
//prove
//x = qy + r(r = x % y)
//n | y, n | r, x = qy + r
//n | x, n | y
//n = gcd(x, y)
int findGcd(int x, int y) {
	return y ? findGcd(y, x % y) : x;
}

int findLcm(int x, int y, int gcd) {
	return x * y / gcd;
}

int main() {
	findGcdAndLcm();
	
	return 0;	
} 

你可能感兴趣的:(C)