马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如题,我是用下面的for循环做的,感觉很麻烦,有简单点的办法吗for i in range(19//8+1):
if 19-8*i >= 6:
for j in range((19-8*i)//6+1):
if 19-8*i-6*j >= 5:
for m in range((19-8*i-6*j)//5+1):
if 19-8*i-6*j-5*m >= 4:
n = (19-8*i-6*j-5*m)//4
print(i, j, m, n)
else:
print(i, j, m, 0)
elif 19-8*i-6*j >= 4:
n = (19-8*i-6*j)//4
print(i, j, 0, n)
else:
print(i, j, 0, 0)
elif 19-8*i >= 5:
for m in range((19-8*i)//5+1):
if 19-8*i-5*m >= 4:
n = (19-8*i-5*m)//4
print(i, 0, m, n)
else:
print(i, 0, m, 0)
elif 19-8*i >= 4:
n = (19-8*i)//4
print(i, 0, 0, n)
else:
print(i, 0, 0, 0)
这个算不算简单一些呢? for i in range(3):
for j in range(4):
for m in range(4):
for n in range(5):
if 16 <= 8 * i + 6 * j + 5 * m + 4 * n <= 19:
print((i , j , m , n))
运行实况: D:\[00.Exerciese.2022]\Python>python x.py
(0, 0, 0, 4)
(0, 0, 1, 3)
(0, 0, 2, 2)
(0, 0, 3, 1)
(0, 1, 0, 3)
(0, 1, 1, 2)
(0, 1, 2, 0)
(0, 2, 0, 1)
(0, 2, 1, 0)
(0, 3, 0, 0)
(1, 0, 0, 2)
(1, 0, 1, 1)
(1, 0, 2, 0)
(1, 1, 0, 1)
(1, 1, 1, 0)
(2, 0, 0, 0)
D:\[00.Exerciese.2022]\Python>
|