lintcode入门篇十四

845. 最大公约数

中文 English

给两个数字,数字 a 跟数字 b。找到两者的最大公约数

样例

样例1

输入: a = 10, b = 15
输出: 5
解释:
10 % 5 == 0
15 % 5 == 0

样例2

输入: a = 15, b = 30
输出: 15
解释:
15 % 15 == 0
30 % 15 == 0

注意事项

在数学意义上, 两个或多个不均为 0 的整数的最大公约数 (gcd) 是可以整除每个给出的整数的最大正整数

输入测试数据 (每行一个参数) 如何理解测试数据?
class Solution:
    """
    @param a: the given number
    @param b: another number
    @return: the greatest common divisor of two numbers
    """
    def gcd(self, a, b):
        # write your code here
        s = b  if a>b else a

        for i in range(s,0,-1):
            if a%i == 0 and b%i == 0:
                return i

猜你喜欢

转载自www.cnblogs.com/yunxintryyoubest/p/12567594.html