gpt4 book ai didi

java - 按比例分配的整个项目

转载 作者:行者123 更新时间:2023-11-30 09:17:31 24 4
gpt4 key购买 nike

目标:

假设我在一个水果种植园有 X 名 worker 。在种植园里,他们种植苹果、梨和葡萄。

在一天结束时,工头用一个比率给每个 worker 打分。所有比率的总和为 100。该比率决定了一天结束时如何在 worker 之间分配水果。

我如何在所有 worker 之间分配水果,以便他们每个人都能得到公平的份额(在一定的随机性内以考虑整数除法)。只有完整的水果被分割,所以整数结果。所有的水果都必须分发。

我正在与大约 20 名 worker 一起做这件事,所以现在这个比率大约是每个 worker 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 用随机 boolean 值初始化并在处理每个水果后切换。

我试过的(完整代码):

public void balance() {
boolean roundUp = random.nextBoolean();
for (Employee e : employees) {
double ratio = e.getRatio();
if (applePlanned < appleNeeded) {
int apple;
if (roundUp) {
apple = (int) Math.ceil(ratio * appleNeeded);
} else {
apple = (int) Math.floor(ratio * appleNeeded);
}
e.setrapple(apple);
applePlanned += apple;
roundUp = !roundUp;
}

if (pearPlanned < pearNeeded) {
int pear;
if (roundUp) {
pear = (int) Math.ceil(ratio * pearNeeded);
} else {
pear = (int) Math.floor(ratio * pearNeeded);
}
e.setrpear(pear);
pearPlanned += pear;
roundUp = !roundUp;
}

if (grapePlanned < grapeNeeded) {
int grape;
if (roundUp) {
grape = (int) Math.ceil(ratio * grapeNeeded);
} else {
grape = (int) Math.floor(ratio * grapeNeeded);
}
e.setrgrape(grape);
grapePlanned += grape;
roundUp = !roundUp;
}
}

遇到的问题:

  • 只有大约 3/4 的元素被分发
  • 当我有偶数个水果时, boolean 值在每个新人开始时获得相同的值。

感谢您调查此事!

请用 java、python 或伪代码回答,这是我能读懂的。

最佳答案

使用双重数学,四舍五入,然后根据比例随机分发剩余的水果。请注意,您可以通过面向对象和循环使它变得不那么难看,但这是一个开始。

public void distribute(int apple, int pear, int grape) {
double total = apple + pear + grape;
double appleRatio = apple/total;
double pearRatio = pear/total;
double grapeRatio = grape/total;

// apple worker
int appleWorkerApple = (int) (appleRatio*apple);
int appleWorkerPear = (int) (appleRatio*pear);
int appleWorkerGrape = (int) (appleRatio*grape);

// pear worker
int pearWorkerApple = (int) (pearRatio*apple);
int pearWorkerPear = (int) (pearRatio*pear);
int pearWorkerGrape = (int) (pearRatio*grape);

// grape worker
int grapeWorkerApple = (int) (grapeRatio*apple);
int grapeWorkerPear = (int) (grapeRatio*pear);
int grapeWorkerGrape = (int) (grapeRatio*grape);

int appleRemain = apple - appleWorkerApple - pearWorkerApple - grapeWorkerApple;
int pearRemain = pear - appleWorkerApple - pearWorkerApple - grapeWorkerApple;
int grapeRemain = grape - appleWorkerApple - pearWorkerApple - grapeWorkerApple;

Random r = new Random();
while(appleRemain > 0 && pearRemain > 0 && grapeRemain > 0) {
double target = r.nextDouble();
switch(r.nextInt(3)) {
case 0:
if(appleRemain > 0) {
appleRemain--
if(target < appleRatio)
appleWorkerApple++;
else if (target < appleRatio + grapeRatio)
pearWorkerApple++;
else
grapeWorkerApple++;
}
break;
case 1:
if(grapeRemain > 0)
// etc.
}
}
}

关于java - 按比例分配的整个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18929109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com