欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

初学者指南:计算最大公约数与最小公倍数的方法

最编程 2024-07-23 21:12:25
...
# 使用辗转相除法 # 最大公约数c: 最小公倍数:a*b/c # a>b; # while (a/b余数c!=0){ # a = b # b = c #} # c为最大公约数 def gcd(a, b): #求最大公约数 if b > a: #交换a和b temp = a a = b b = temp c = a % b while c!=0: a = b b = c c = a%b return b def lcm(a, b): #求最小公倍数 temp = gcd(a, b) return a*b/temp a, b = map(int,input().strip().split()) temp1 = gcd(a,b) temp2 = lcm(a,b) temp2 = int(temp2) print(temp1, temp2)

推荐阅读