我正在尝试为一堆用户计算游戏分数,但我还没有得到。这是一个金字塔游戏,您可以邀请其他人,并且您邀请的人被放置在关系树中。

因此,如果我邀请X和X邀请Y,我将从他们两个那里获得回扣。假设10%^个步骤...

所以从X我得到他分数的10%,从Y得到1%,而X从Y得到10%。

因此,为了计算这一点,我认为每个“玩家”都有一个计算其总得分的函数。该函数必须是递归的,并且“知道”它在树中的距离,以便可以踢回正确的值。

def get_score(player):
    if children:
        score = player.points
        for child in children:
            score += child.points*math.pow(.1, get_ancestors(child))
            score += get_score(child)
        return score
    else:
        return player.points


但这不能正常工作,它提供了我认为某些级别的正确价值观,而不是其他级别的正确价值观。所以以为我的功能坏了。有人知道如何解决这个问题吗?

最佳答案

我怀疑这两行

score += child.points*math.pow(.1, get_ancestors(child))
score += get_score(child)


这是一个简单的递归结构,所以我认为下面的内容就足够了

score += get_score(child)*.1


递归的美将照顾自己

您也不需要“如果孩子:”检查
有帮助吗

def get_score(player):
    score = player.points
    for child in children:
        score += get_score(child)*.1
    return score

10-07 19:12
查看更多