很简单但困扰我很久的问题:1/a+1/b=1/6,五个答案,为什么只有跑出四个?到底哪里错了?
初始化一个列表来存储解#
solutions = []
遍历可能的 a 和 b 的值#
for a in range (6, 100): # 从 6 开始遍历
for b in range (a, 100): # b 从 a 开始,因为 a 和 b 是无序的,且我们不想重复计算
if 1/a + 1/b == 1/6:
solutions.append((a, b))
打印所有解#
for solution in solutions:
print(f”a = {solution[0]}, b = {solution[1]}”)
#解 1、a=7,b=42;解 2、a=8,b=24; 解 3、a=9,b=18; 解 4、a=10,b=15; 解 5、a=12,b=12。解 4 就是跑不出来,到底是哪里错了呢?
推荐文章: