求最大公约数
def comparative_subtraction(j, k):
count = 0
while k % 2 == 0 and j % 2 == 0:
k, j, count = int(k / 2), int(j / 2), count + 1
while True:
if k == j:
print("最大公约数为:{0}".format(2 ** count * k))
return
kj = abs(k - j)
if k > j:
k = kj
else:
j = kj
def division_by_rolling(j, k):
while True:
if k > j:
j, k = k, j
r = j % k
if r == 0:
print("最大公约数为:{0}".format(k))
return
j = k
k = r
if __name__ == '__main__':
import math
try:
a, b = map(int, input("请输入两个正整数,中间用空格分开\n").split())
if a <= 0 or b <= 0:
print("输入错误!")
exit()
comparative_subtraction(a, b)
division_by_rolling(a, b)
print("最大公约数为:{0}".format(math.gcd(a, b)))
except ValueError:
print("输入错误!")
转载请注明原文地址:https://ipadbbs.8miu.com/read-21104.html