世界杯小组赛积分所有可能情况

来源:互联网 发布:老白老婆 知乎 编辑:程序博客网 时间:2024/05/01 03:28

    学习python中。前两天写了一小段代码,列出所有可能的世界杯小组积分情况,并计算在胜负平等概率时各种积分的出现的概率。

    大体思路是用递归得到所有3^6=729种情况,然后利用dict数据类型进行归纳,并进行排序输出。

结果:

[3, 3, 3, 3] : (0.14%)
[4, 4, 4, 3] : (1.10%)
[4, 4, 4, 4] : (0.82%)
[5, 3, 3, 2] : (1.65%)
[5, 4, 3, 2] : (3.29%)
[5, 4, 4, 2] : (3.29%)
[5, 4, 4, 3] : (3.29%)
[5, 5, 2, 2] : (1.65%)
[5, 5, 3, 1] : (1.65%)
[5, 5, 3, 2] : (1.65%)
[5, 5, 4, 1] : (3.29%)
[5, 5, 5, 0] : (0.55%)
[6, 4, 4, 2] : (3.29%)
[6, 4, 4, 3] : (4.94%)
[6, 5, 2, 2] : (1.65%)
[6, 5, 4, 1] : (3.29%)
[6, 6, 3, 3] : (3.29%)
[6, 6, 4, 1] : (3.29%)
[6, 6, 6, 0] : (1.10%)
[7, 3, 2, 2] : (1.65%)
[7, 4, 2, 2] : (3.29%)
[7, 4, 3, 1] : (3.29%)
[7, 4, 3, 2] : (3.29%)
[7, 4, 3, 3] : (3.29%)
[7, 4, 4, 1] : (4.94%)
[7, 5, 2, 1] : (3.29%)
[7, 5, 3, 1] : (3.29%)
[7, 5, 4, 0] : (3.29%)
[7, 6, 2, 1] : (3.29%)
[7, 6, 3, 1] : (3.29%)
[7, 6, 4, 0] : (3.29%)
[7, 7, 1, 1] : (0.82%)
[7, 7, 3, 0] : (1.65%)
[9, 2, 2, 2] : (0.55%)
[9, 3, 3, 3] : (1.10%)
[9, 4, 2, 1] : (3.29%)
[9, 4, 3, 1] : (3.29%)
[9, 4, 4, 0] : (1.65%)
[9, 6, 1, 1] : (1.65%)
[9, 6, 3, 0] : (3.29%)
Total possibilities: 40

代码:

global p
global result
global points
global N

def simu(x, y):

    for n in range(0, 3):
        points[x][y] = p[n]
        points[y][x] = p[2 - n]
        if x == N-2 and y == N-1:
            temp = [sum(points[i]) for i in range(0, N)]
            temp.sort(reverse=True)
            if result.has_key(str(temp)):
                result[str(temp)] += 1
            else:
                result[str(temp)] = 1
        else:
            if (y == N-1):
                simu(x+1, x+2)
            else:
                simu(x, y+1)

if __name__ == "__main__":

    N = 4 # there are 4 teams in every group
    p = [3, 1, 0] # points when [win, draw, lose]
    result = {}
    points = [[0] * N for i in range(0, N)] # matrix of points
    simu(0, 1) # get all possibilities
    matches = 100.0 / (3 ** (N * (N - 1) / 2)) # percents of each possibility
    new = []
    for k, v in result.items():
        new.append("%s : (%2.2f%%)" % (k, v * matches))
    print '/n'.join(sorted(new))
    print "Total possibilities: %d" % len(result)

原创粉丝点击