所以我正在做的这个项目就是,根据球队的比赛次数和他们的得分,找出一个赛季中所有的胜负组合。你知道吗
到目前为止,我只知道如何获得一个组合,但我不确定我将如何着手获得所有的组合。你知道吗
赢3分,平1分,输0分。
举个例子,球队打了20场比赛,得到了30分,我的结果应该是:
10-0-10
9-3-8
8-6-6
7-9-4
6-12-2
5-15-0
这就是我目前拥有的:
def process_seasons(seasons):
# Repeating this until all seasons have been processed
for i in range(len(seasons)):
games = seasons[i][0]
points = seasons[i][1]
curSeason = i + 1
gamesWon=gamesTied=gamesLost=0
# default cases
if (points % 3 == 0):
gamesWon = points/3
process_season(curSeason, games, points, gamesWon, gamesTied, gamesLost)
if (points == 0):
gamesLost = games
process_season(curSeason, games, points, gamesWon, gamesTied, gamesLost)
# combination cases
pass
函数是这样调用的:
# format of list: [[season-1-games, season-1-points], [season-2-games, season-2-points], etc.]
soccer_seasons = [[1, 3], [1, 1], [1, 0], [20, 30]]
process_seasons(soccer_seasons)
使用
itertools.product
:因此,在本例中,第一个解决方案(多数赢=最少的游戏值点数)将非常简单地是
wins = total // 3
,ties = total % 3
,losses = games - wins - ties
显然,如果我们有
losses < 0
,那么我们在这里失败并返回空列表。你知道吗否则,您可以使用第一个解决方案并迭代:
在你得到
wins < 0
或losses < 0
之前,这不会改变本赛季的总积分或总比赛次数在代码中:
请注意,这种简单的解决方案是可行的,因为点值的结果很好。一般情况是NP难的(参见:https://en.wikipedia.org/wiki/Change-making_problem)
相关问题 更多 >
编程相关推荐