python分配_(Python)平均分配数字

与您提供的示例一致的一种算法如下:

让平均战利品成为攻击者的潜力除以防御者非零资源的数量。循环通过防御者的非零资源,如果其中任何一个小于或等于平均战利品,将其从防御者中移除并将其交给攻击者。

如果在步骤1中遇到并移动了小于或等于平均战利品的资源,则重新计算平均战利品并重复步骤1.否则,请继续执行步骤3.

最后,如果防御者还剩下任何资源,只需重新计算平均战利品并将其从每个资源中移除(将其交给攻击者)。

python中可能的实现如下:

def step1(dres, aloot, potential):

again = False

ndres = {}

if len(dres) > 0:

avgloot = int(potential / len(dres))

for r in dres:

if dres[r] <= avgloot:

potential -= dres[r]

aloot[r] += dres[r]

again = True

else:

ndres[r] = dres[r]

return (ndres, aloot, potential, again)

def calculate_loot(dres, potential):

aloot = {'wood':0, 'iron':0, 'wheat':0, 'gold':0}

(dres, aloot, potential, again) = step1(dres, aloot, potential)

while again:

(dres, aloot, potential, again) = step1(dres, aloot, potential)

if len(dres) > 0:

avgloot = int(potential / len(dres))

for r in dres:

aloot[r] += avgloot

return aloot

potential = 250

for dres in [

{'wood':100, 'iron':25, 'wheat':76, 'gold':50000},

{'wood':2500, 'iron':2500, 'wheat':5000, 'gold':5000 },

{'wood':100, 'iron':0, 'wheat':1, 'gold':0 },

{'wood':0, 'iron':0, 'wheat':0, 'gold':50000}

]:

print(dres)

print(calculate_loot(dres, potential))

print(' ')Online Demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值