Python 最大公约数算法
Python 3 教程
· 2019-02-12 13:28:45
以下代码用于实现最大公约数算法:
实例(Python 3.0+)
# Filename : test.py
# author by : www.codercto.com
# 定义一个函数
def hcf(x, y):
"""该函数返回两个数的最大公约数"""
# 获取最小值
if x > y:
smaller = y
else:
smaller = x
for i in range(1,smaller + 1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
# 用户输入两个数字
num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))
print( num1,"和", num2,"的最大公约数为", hcf(num1, num2))
执行以上代码输出结果为:
输入第一个数字: 54 输入第二个数字: 24 54 和 24 的最大公约数为 6
点击查看所有 Python 3 教程 文章: https://www.codercto.com/courses/l/10.html
Zero to One
Peter Thiel、Blake Masters / Crown Business / 2014-9-16 / USD 27.00
“This book delivers completely new and refreshing ideas on how to create value in the world.” - Mark Zuckerberg, CEO of Facebook “Peter Thiel has built multiple breakthrough companies, and ......一起来看看 《Zero to One》 这本书的介绍吧!