最快接力时间=最低可能的总整数值(自由泳运动员的时间)+ (1名游泳运动员的时间)+(另一名仰泳运动员的时间)++(另一名蝶泳运动员的时间)
输入数据:字典数组,其中每个字典代表每一笔的游泳者游泳时间。每本词典都有“飞”、“回”、“胸”、“自由”的命名属性:
示例输入:
[
{'Fly': 10, 'Breast': 48.26, 'Free': 28.43, 'Back': 34},
{'Fly': 47.51, 'Breast': 24, 'Free': 28.47, 'Back': 10},
{'Fly': 26, 'Breast': 46.44, 'Free': 10, 'Back': 30},
{'Fly': 37.81, 'Breast': 10, 'Free': 24, 'Back': 49},
{'Fly': 38.31, 'Breast': 29, 'Free': 20, 'Back': 10}
]示例输出:
40 (这是一个简单的例子,最好的接力赛只是每一次划水中最快的人;其他时候,你可能需要第三次最快的运动员游泳短泳,这样才能获得最低的总时间!)
使用上述数据集中的前4位游泳者测量继电器的示例:
swimmer 1 swims fly and his time is 10
total += 10
swimmer 2 swims back and his time is 10
total += 10
swimmer 3 swims fly and his time is 10
total += 10
swimmer 4 swims breast and his time is 10
best relay total for these 4 people = 40输出数据:应该是单个浮点数或整数值,表示最佳中继的总时间。(法定接力赛包括一名游泳运动员、一名后座游泳运动员、一名乳房游泳运动员和一名自由游泳运动员,接力赛的每一条腿都由一名独特的游泳运动员游泳)
编辑:蛮力与动态参数移除。
发布于 2014-06-23 17:30:56
嗯,我不是Python程序员。有人甚至会犹豫叫我程序员。但是,由于没有其他人回答这个问题,为什么不张贴我的解决方案呢?如果说还有什么其他的话,那么简单地回顾一下Python的基本知识就很有趣了:
swimmers = [{'Fly': 10, 'Breast': 48.26, 'Free': 28.43, 'Back': 34},
{'Fly': 47.51, 'Breast': 24, 'Free': 28.47, 'Back': 10},
{'Fly': 26, 'Breast': 46.44, 'Free': 10, 'Back': 30},
{'Fly': 37.81, 'Breast': 10, 'Free': 24, 'Back': 49},
{'Fly': 38.31, 'Breast': 29, 'Free': 20, 'Back': 10}]
bestTime = float('inf')
for fly in range(len(swimmers)):
for breast in range(len(swimmers)):
for free in range(len(swimmers)):
for back in range(len(swimmers)):
if len(set([fly,breast,free,back])) > 3:
current = swimmers[fly]['Fly'] + swimmers[breast]['Breast'] + swimmers[free]['Free'] + swimmers[back]['Back']
if current < bestTime:
bestTime = current
print bestTimehttps://codegolf.stackexchange.com/questions/32280
复制相似问题