1008:最大公约数

题目描述

Pile 想要知道一个经典问题:
给你两个数,请求出一个数的最大公约数

解答:

a=int(input())
b=int(input())
min=a
if(a>b):
    min=b
while min>0:
    if(a%min==0 and b%min==0):
        break;
    min=min-1
print(min)

若使用循环,则会时间超限,原因是系统给的样例很大(如100000)

可以调用math库里的gcd函数来编程解决

import math#调用math库
a=int(input())
b=int(input())
print(math.gcd(a,b))#求最大公约数

关于gcd函数:

math.gcd() method is a library method of math module, it is used to find GCD (Greatest Common Divisor) of given numbers, it accepts two integer numbers and returns their greatest common divisor (the largest positive integer that divides both of the numbers).

math.gcd()方法数学模块的库方法,用于查找给定数字的GCD(最大公约数),它接受两个整数并返回其最大公约数(将两个整数相除的最大正整数)。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值