我正在尝试制定用于优化日常任务计划的算法。 例如:
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);
}
它返回了我的工人人数,但是却无法正常工作。我没有像在这几个示例中写的那样得到结果。 你能给我一些建议吗?
答案 0 :(得分:1)
我不确定您是否可以找到确切的公式,所以我会做简单的暴力破解:
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>
}