求16<= 8i+6j+5m+4n <= 19的整数解解集
如题,我是用下面的for循环做的,感觉很麻烦,有简单点的办法吗{:10_269:}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:\\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:\\Python>
页:
[1]