擅长:python、mysql、java
<p>这里有一个小程序,找到正确的组合重量板。注意函数zip,它将配重板的数量列表与配重列表相结合。<code>list(zip(nweights, weights))</code>形成元组列表,例如<code>[(4, 100), (0, 45) ... (0, 2), (0, 2.5)]</code></p>
<pre><code>weights=[100, 45, 35, 25, 10, 5, 2, 2.5]
targetweight = int(input('What is the target weight: '))
nweights = []
remaining = targetweight
for i, weight in enumerate(weights):
nweights.append(int(remaining/ weight))
remaining = remaining - nweights[i]*weights[i]
if remaining == 0:
break
listweights=zip(nweights, weights)
for weight in listweights:
print(f'you need {weight[0]} of weight {weight[1]} pound')
if remaining !=0:
print(f'the correct weight combination cannot be found,'
f'the remaining weight is: {remaining} pound')
</code></pre>