gpt4 book ai didi

java - 高效优化算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:06:20 25 4
gpt4 key购买 nike

我正在尝试制定优化每日任务计划的算法。例如:

1. There are 16 tasks to finish in one day, expirience worker can finish 8 tasks and newer worker could finish 4 tasks. As output we should get 2 expirienced worker and none new worker.

现在我有这段代码,但它不能正常工作:

private void calculate(int numberOfTasks, int oldWorkerValue, int youngWorkerValue) {

int numberOfOldWorkers = numberOfTasks / oldWorkerValue;
int numberOfYoungWorkers = 0;

if(numberOfOldWorkers == 0) {
numberOfOldWorkers = 1;
} else {
numberOfYoungWorkers = (numberOfTasks % numberOfOldWorkers) / youngWorkerValue;

if(numberOfTasks % oldWorkerValue != youngWorkerValue*numberOfYoungWorkers) {
numberOfYoungWorkers = numberOfYoungWorkers + 1;
}
}
System.out.println("Expirienced worker: " + numberOfOldWorkers + " and new workers: " + numberOfYoungWorkers);
}

它返回我的工作人员数量,但它不能正常工作。我没有得到像我在这几个例子中写的那样的结果。你能给我一些建议吗?

最佳答案

我不确定你是否能找到确切的公式,所以我会做简单的暴力破解:

public static Pair<Integer, Integer> calculate(int numberOfTasks, int oldWorkerValue, int youngWorkerValue) {
int m = Integer.MAX_VALUE; // minimal value
int o = 0; // old workers
int n = 0; // new workers
for (int i = 1; i <= (int)Math.ceil((double) numberOfTasks / oldWorkerValue); i++) {
int nw = (int) Math.ceil((double)(numberOfTasks - i * oldWorkerValue) / youngWorkerValue);
if (nw < 0) nw = 0;
int tm = i * oldWorkerValue + nw * youngWorkerValue;
if (tm < m) {
m = tm; o = i; n = nw;
}
}
return new Pair<>(o, n); // pair <old workers, new workers>
}

关于java - 高效优化算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56995322/

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