这只是一个有趣的项目,我认为做起来很酷,但我一直在努力找出答案。
plates = [100, 45, 35, 25, 10, 5, 2.5]
goal_weight = 425
starting_weight = 45
while goal_weight > starting_weight:
我的想法是用while循环遍历板块。我需要每个数字以最大程度地达到目标权重(100进入450次4次),然后移至下一个数字并尝试在其中进行尝试,以显示加载杆的理想方式。但是我在这里可能走错了路。
示例:250 = 45lb bar(starting_weight),两个100lb板,两个2.5lb板 425 = 45lb bar,两个100lb,四个45lb
想要打印类似的内容:两个100,两个45,两个10
答案 0 :(得分:0)
这里有个小程序,可以找到合适的重量盘组合。请注意功能zip,该功能zip将重量板数量列表和重量列表结合在一起。 list(zip(nweights, weights))
形成一个元组列表,例如[(4, 100), (0, 45) ... (0, 2), (0, 2.5)]
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')
答案 1 :(得分:0)
这就是我最终得到的。谢谢大家的帮助!
weights=[100, 45, 35, 25, 10, 5, 2.5]
target_weight = int(input('How much weight do you need? '))
bar_weight = int(input('Enter bar weight: '))
nweights = []
remaining = target_weight - bar_weight
for i, weight in enumerate(weights):
if int(remaining / weight) % 2 == 0:
nweights.append(int(remaining/ weight))
else:
nweights.append(int(remaining/ weight) - 1)
remaining = remaining - nweights[i]*weights[i]
if remaining == 0:
break
listweights=zip(nweights, weights)
print(f'{bar_weight}lb bar')
for weight in listweights:
if weight[0] >= 2:
print(f"{weight[0]} | {weight[1]}'s")
if remaining !=0:
print(f'the correct weight combination cannot be found,'
f'the remaining weight is: {remaining} pound')
答案 2 :(得分:0)
是的,我认为您的解决方案有效,尽管以下代码段可能更合逻辑... (使用一些numpy数组方法)
$str = 'DisplayText="cf font="Arial" size="10" complexscriptsfont="Arial" complexscriptssize="10" asiantextfont="Arial" fontcolor="595959"">';
$splitted = explode('"', $str);
$splittedSize = count($splitted);
$result = $splitted[0].'"'.implode('"',array_slice($splitted,1,$splittedSize-2)).$splitted[$splittedSize-1].'"';
echo $result;