辗转相除法:
(1)比较两数,并使m>n
(2)将m作被除数,n做除数,相除后余数为r
(3)循环判断r,若r==0,则n为最大公约数,结束循环。若r !=0 ,执行m=n,n=r;将m作被除数,n做除数,相除后余数为r
运行代码如下:
num1 = int(input("请输入第一个数字:"))num2 = int(input("请输入第一个数字:"))m = max(num1, num2)n = min(num1, num2)r = m % nwhile r != 0:m = nn = rr = m % nprint(num1, "和", num2, "的最大公约数为", n)