现在的位置: 首页 > 综合 > 正文

Solving Greatest Common Divisor

2014年09月05日 ⁄ 综合 ⁄ 共 298字 ⁄ 字号 评论关闭

Two ways of solving greatest common divisor: recursion and iteration.

#include <algorithm>
using namespace std;

int maxCommonDivisor1(int a, int b) {
	int n = max(a, b), d = min(a, b), m = n % d;
	return (m==0)? d: maxCommonDivisor1(d, m);
}

int maxCommonDivisor2(int a, int b) {
	int n = max(a, b), d = min(a, b), m = n % d;
	while (m != 0) {
		n = d;
		d = m;
		m = n % d;
	}
	return d;
}

【上篇】
【下篇】

抱歉!评论已关闭.