【发布时间】:2015-07-14 02:29:20
【问题描述】:
我有一个字符串列表,每行有 20 个字符串。第一行表示“任务”,第二行表示“任务之间的依赖关系”,第三行表示“每个任务的小时数”。我想根据 3 个标准订购它。我还想将此任务分配给 2 个开发人员(我称他们为 Cp1 和 Cp2)。
我的标准:
如果字符串 X 等于 0,则将其添加到开发人员,并减少小时数。
如果任务依赖已经执行(完成),则以更少的时间将其添加给开发人员。
如果任务依赖阻止了下一个任务并且没有更多的任务没有依赖,计算非活动小时数将其添加到开发人员的小时数较少。
我的输入有 3 行:
t14 t18 t7 t12 t15 t5 t16 t10 t4 t19 t20 t6 t1 t13 t9 t8 t11 t17 t2 t3
t17 t2 t4 t16 t18 t4 t19 t4 0 t5 t1 t4 0 0 t2 t5 t14 t19 0 0
4 8 2 4 8 40 2 2 4 2 24 2 6 8 2 2 5 10 16 16
我的结果应该是:
Cp1:t4 t7 t10 t20 t6 t13 t2 t14 t18 t15 - 78 小时
Cp2: t1 t5 t19 t16 t12 t8 t17 t9 t11 t3 - 89
我的代码给出了错误的输出:
Cp1: [t4, t2, t4, t1, t9, t2, t4, t1, t9, t2, t4, t1, t9, t2, t4, t1, t9, t2, t4, t1, t9, t2, t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4, t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1, t9, t2, t4, t1, t9, t2] 小时:552
Cp2: [t1, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20] 小时: 550
为什么我的代码给出了超过 20 个字符串?它应该给我分配在两个 CP 之间的 20 个字符串。
为什么我的代码没有给出正确的顺序?
我的代码:
public int calcFitness(int indv) {
String linha1 = individualID.get(indv);
idQe = linha1.split(" ");
String linha2 = individualDep.get(indv);
depQe= linha2.split(" ");
String linha3 = individualHour.get(indv);
hourQe = linha3.split(" ");
String linha4 = individualEmpl.get(indv);
emplQe = linha4.split(" ");
int count1 =0;
int count2 =0;
int idle =0;
for (int x=0; x<idQe.length;x++){
// int y = x + 1;
for(int j=0;j<depQe.length;j++){
if(depQe[j].equals("0")){
if (count1<=count2){
Cp1.add(idQe[j]);
Cp1Hour.add(hourQe[j]);
Cp1Dep.add(depQe[j]);
count1 += Integer.parseInt(hourQe[j]);
}
else if(count2<count1){
Cp2.add(idQe[j]);
Cp2Hour.add(hourQe[j]);
Cp2Dep.add(depQe[j]);
count2 += Integer.parseInt(hourQe[j]);
}
}
else if ((Cp1.contains(depQe[j])||(Cp2.contains(depQe[j])))){
if (count1<=count2){
if (!Cp1.contains(depQe[j])){
idle = Math.abs(count2-count1);
count1 += idle;
Cp1.add(idQe[j]);
Cp1Hour.add(hourQe[j]);
Cp1Dep.add(depQe[j]);
count1 += Integer.parseInt(hourQe[j]);
}
else {
Cp1.add(idQe[j]);
Cp1Hour.add(hourQe[j]);
Cp1Dep.add(depQe[j]);
count1 += Integer.parseInt(hourQe[j]);
}
}
else if(count2<count1){
if (!Cp2.contains(depQe[j])){
idle = Math.abs(count1-count2);
count2 += idle;
Cp2.add(idQe[j]);
Cp2Hour.add(hourQe[j]);
Cp2Dep.add(depQe[j]);
count2 += Integer.parseInt(hourQe[j]);
}
else{
Cp2.add(idQe[j]);
Cp2Hour.add(hourQe[j]);
Cp2Dep.add(depQe[j]);
count2 += Integer.parseInt(hourQe[j]);
}
}
}
j++;
}
}
我对代码进行了一些更改。我现在的问题是添加新字符串后如何开始读取第一个字符串?
我想在添加新字符串后从头开始阅读列表。但是,如果已经添加了这样的字符串,我想跳到下一个。我怎么能那样做?最后,我希望有 20 个字符串分布在 2 个开发人员或列表中。
我的代码:
for (int taskIndex = 0; taskIndex < taskQe.length; taskIndex++) {
String currentTask = taskQe[taskIndex];
String currentDep = depQe[taskIndex];
String currentHour = hourQe[taskIndex];
System.out.println("task: [" + currentTask + "], dep: [" + currentDep + "], hours: [" + currentHour + "]");
int hour = Integer.parseInt(currentHour);
if (currentDep.equals("0")) {
// System.out.println ("axhou 0");
if (Cp1TotalHours <= Cp2TotalHours) {
Cp1.add(currentTask);
Cp1Hour.add(hour);
Cp1Dep.add(currentDep);
Cp1TotalHours += hour;
} else {
Cp2.add(currentTask);
Cp2Hour.add(hour);
Cp2Dep.add(currentDep);
Cp2TotalHours += hour;
}
} else if (Cp1.contains(currentDep) || Cp2.contains(currentDep)) {
//System.out.println ("achou dep" + currentDep );
for (int i=0; i<=taskIndex; i++) {
if (depQe[i].equals(currentDep)){
System.out.println ("achou dep i and curr" + (taskQe[i]) + currentDep );
if ((Cp1.contains(currentDep)&& (Cp1TotalHours <= Cp2TotalHours))){
Cp1.add(taskQe[i]);
Cp1Hour.add(Integer.parseInt(hourQe[i]));
Cp1Dep.add(depQe[i]);
Cp1TotalHours += (Integer.parseInt(hourQe[i]));
}
else if ((Cp2.contains(currentDep)&& (Cp2TotalHours < Cp1TotalHours))){
Cp2.add(taskQe[i]);
Cp2Hour.add(Integer.parseInt(hourQe[i]));
Cp2Dep.add(depQe[i]);
Cp2TotalHours += (Integer.parseInt(hourQe[i]));
}
else if ((!Cp1.contains(currentDep)&& (Cp1TotalHours <= Cp2TotalHours))){
Cp1.add(taskQe[i]);
Cp1Hour.add(Integer.parseInt(hourQe[i]));
Cp1Dep.add(depQe[i]);
Cp1TotalHours += (Integer.parseInt(hourQe[i]));
/* for (int j=0; j<taskQe.length; j++) {
if(depQe[i].equals(taskQe[j]))
idle = Math.abs(Integer.parseInt(hourQe[j]) - (Integer.parseInt(hourQe[i])));
Cp1TotalHours=+idle;
}*/
}
else if ((!Cp2.contains(currentDep)&& (Cp2TotalHours < Cp1TotalHours))){
Cp2.add(taskQe[i]);
Cp2Hour.add(Integer.parseInt(hourQe[i]));
Cp2Dep.add(depQe[i]);
Cp2TotalHours += (Integer.parseInt(hourQe[i]));
/* for (int j=0; j<taskQe.length; j++) {
if(depQe[i].equals(taskQe[j]))
idle = Math.abs(Integer.parseInt(hourQe[j]) - (Integer.parseInt(hourQe[i])));
Cp2TotalHours=+idle;
}*/
}
}
}
}
【问题讨论】:
-
那么,你的问题是什么?
-
我的代码不起作用。最后我的输出是: >Cp1: [t4, t2, t4, t1, t9, t2, t4, t1, t9, t2, t4, t1, t9, t2, t4, t1, t9, t2, t4, t1 ,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9 ,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2 , t4, t1, t9, t2, t4, t1, t9, t2] 小时: 552 >Cp2: [t1, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7, t20, t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7, t20] 小时:550
-
使用对象!这将使您的生活更加简单。拥有
Task和Developer并相应地读取您的数据。 Java 是 OO,发挥它的威力。 -
另外,Java 中的变量位于
camelCase。 -
@Boris 所说的使用对象将让事情变得更简单。