# 微软笔试题-Greatest Common Divisor(non-recursive version)请赐教

Greatest Common Divisor
For two positive integers m and n their greatest common divisor (gcd) is defined as a greatest integer dividing both of them. For instance
gcd(42, 54) = 6
The following recursive function calculates the gcd.
int GCD(int m, int n)
{
int rem = m % n;
return (rem == 0) ? n : GCD(n, rem);
}
The check to see if m and n are positive has been omitted for clarity.
Write a non-recursive version of this function.
...全文
407 4 打赏 收藏 转发到动态 举报

4 条回复

RiqueZhang 2015-10-09
• 打赏
• 举报


int gcd(int m, int n)
{
while (0 != n) {
int r = m % n;
m = n;
n = r;
}
return m;
}

li_x1979 2015-05-06
• 打赏
• 举报

int gcd(int m,int n) { while (true) { if (n == 0) return m; else m = m % n; if (m == 0) return n; else n = n % m; } }
• 打赏
• 举报

While (rem=m%n)!=0 n=rem m=n return n 楼主，你这个内核程序员给出的微软面试题貌似都很简单啊！ FancyMouse 2015-04-11
• 打赏
• 举报 实用最新版Python小白零基础入门教程
Python语言基础，从安装，到变量、循环、列表、元组、字典、异常、类、对象、文本处理等核心知识，配套课堂作业、笔试题等，全程手把手带你写代码。 Greatest Greatest Common Divisor
Greatest Greatest Common Divisor 声网日常实习C++笔试题：求数组的最大公约数 Greatest Common Divisor (GCD) - Euclidean algorithm
The Euclidean algorithm calculates the greatest common divisor (GCD) of two natural numbers a and b. The greatest common divisor g is the largest natural number that divides both a and b without lea 前端经典面试题（一）

32,944 发帖 与我相关 我的任务 • 近7日
• 近30日
• 至今