假设我在一个水果园有X个工人。在种植园里,他们种苹果、梨和葡萄。在
一天结束时,工头按比例给每个工人打分。所有比率的总和是100。这个比率是用来决定如何在一天结束时把水果分给工人。在
我如何在所有工人中分配水果,以便他们每个人都能得到公平的份额(在一定的随机性范围内考虑整数除法)。只有整个水果被分割,所以整数结果。所有的水果都必须分发出去。在
我用大约20个工人来做这个,所以现在这个比率大约是每个工人0.05。在
for each worker:
if applesGiven < appleStock:
worker.give(ratio * applestock);
if pearsGiven < pearStock:
worker.give(ratio * pearStock);
if grapesGiven < grapeStock:
worker.give(ratio * grapeStock);
我会让[fruit]的确切数字由一个boolean Roundup
来确定,这个boolean Roundup
被初始化为一个随机的布尔值,并在每个水果被处理后进行切换。在
谢谢你的调查!
请用java、python或伪代码回答,这是我能读到的。
它不是特别有用,因为我已经过度使用了Numpy,但我会分享,因为它是相关的
使用双倍数学,四舍五入,然后根据比率随机分发剩余的水果。请注意,使用对象方向和循环可以使其不那么难看,但这只是一个开始。在
相关问题 更多 >
编程相关推荐