Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重试91个元素的组合,直到满足条件_Java - Fatal编程技术网

Java 重试91个元素的组合,直到满足条件

Java 重试91个元素的组合,直到满足条件,java,Java,我正在开发一个软件,我需要创建一个由9名玩家组成的阵容,并制定一套标准,我开始怀疑这是一个不可能的问题,还是没有足够的计算能力在有用的时间内解决它。 如果有人能在这方面帮助或指导我,我将非常感谢他们。 一个简单的伪代码或逻辑指导将是完美的 以下是标准: 来自6支不同球队的91名球员,每支球队都有费用。 5个姿势:其中4个是双人姿势,另一个是单人姿势。 配对位置不能有同一原始队的两名球员。 阵容中同一队的球员不可能超过3名。 阵容的最终成本不能超过60000英镑$ 我知道我会对极其重复和不必要的代

我正在开发一个软件,我需要创建一个由9名玩家组成的阵容,并制定一套标准,我开始怀疑这是一个不可能的问题,还是没有足够的计算能力在有用的时间内解决它。 如果有人能在这方面帮助或指导我,我将非常感谢他们。 一个简单的伪代码或逻辑指导将是完美的

以下是标准: 来自6支不同球队的91名球员,每支球队都有费用。 5个姿势:其中4个是双人姿势,另一个是单人姿势。 配对位置不能有同一原始队的两名球员。 阵容中同一队的球员不可能超过3名。 阵容的最终成本不能超过60000英镑$

我知道我会对极其重复和不必要的代码感到厌恶,但我太偏执了

int totalSal = 0;
    while (numPG != 2 && numSG != 2 && numC != 1 && numPF != 2 && numSF != 2 && totalSal < 60000) {//enquanto o salario total < 60000
        for (int i = 0; i < listOfPlayers.getNumPlayers(); i++) {//para cada jogador
            if (team1 == 3) {
                t1blocked = true;
            } else if (team2 == 3) {
                t2blocked = true;
            } else if (team3 == 3) {
                t3blocked = true;
            } else if (team4 == 3) {
                t4blocked = true;
            } else if (team5 == 3) {
                t5blocked = true;
            } else if (team6 == 3) {
                t6blocked = true;
            }
            System.out.println("Seeing player number " + i);
            if (numPG < 2 && totalSal <= 20000) {
                Player selected = listOfPlayers.getPlayerAt(i);
                if (selected.getPos().contains("PG") && selected.getSalary() <= 15000 && selected.getUsed() == false) {
                    if (selected.getTeam().contains("POR") && !t1blocked) {
                        team1++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPG++;
                    } else if (selected.getTeam().contains("OKC") && !t2blocked) {
                        team2++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPG++;
                    } else if (selected.getTeam().contains("WAS") && !t3blocked) {
                        team3++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPG++;
                    } else if (selected.getTeam().contains("PHO") && !t4blocked) {
                        team4++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPG++;
                    } else if (selected.getTeam().contains("LAL") && !t5blocked) {
                        team5++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPG++;
                    } else if (selected.getTeam().contains("DAL") && !t6blocked) {
                        team6++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPG++;
                    }
                }
            }
            if (team1 == 3) {
                t1blocked = true;
            } else if (team2 == 3) {
                t2blocked = true;
            } else if (team3 == 3) {
                t3blocked = true;
            } else if (team4 == 3) {
                t4blocked = true;
            } else if (team5 == 3) {
                t5blocked = true;
            } else if (team6 == 3) {
                t6blocked = true;
            }
            if (numSG < 2 && totalSal <= 30000) {
                Player selected = listOfPlayers.getPlayerAt(i);
                if (selected.getPos().contains("SG") && selected.getSalary() <= 15000 && selected.getUsed() == false) {
                    if (selected.getTeam().contains("POR") && !t1blocked) {
                        team1++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSG++;
                    } else if (selected.getTeam().contains("OKC") && !t2blocked) {
                        team2++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSG++;
                    } else if (selected.getTeam().contains("WAS") && !t3blocked) {
                        team3++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSG++;
                    } else if (selected.getTeam().contains("PHO") && !t4blocked) {
                        team4++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSG++;
                    } else if (selected.getTeam().contains("LAL") && !t5blocked) {
                        team5++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSG++;
                    } else if (selected.getTeam().contains("DAL") && !t6blocked) {
                        team6++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSG++;
                    }
                }
            }
            if (team1 == 3) {
                t1blocked = true;
            } else if (team2 == 3) {
                t2blocked = true;
            } else if (team3 == 3) {
                t3blocked = true;
            } else if (team4 == 3) {
                t4blocked = true;
            } else if (team5 == 3) {
                t5blocked = true;
            } else if (team6 == 3) {
                t6blocked = true;
            }
            if (numSF < 2 && totalSal <= 40000) {
                Player selected = listOfPlayers.getPlayerAt(i);
                if (selected.getPos().contains("SF") && selected.getSalary() <= 15000 && selected.getUsed() == false) {
                    if (selected.getTeam().contains("POR") && !t1blocked) {
                        team1++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSF++;
                    } else if (selected.getTeam().contains("OKC") && !t2blocked) {
                        team2++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSF++;
                    } else if (selected.getTeam().contains("WAS") && !t3blocked) {
                        team3++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSF++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSF++;
                    } else if (selected.getTeam().contains("PHO") && !t4blocked) {
                        team4++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSF++;
                    } else if (selected.getTeam().contains("LAL") && !t5blocked) {
                        team5++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSF++;
                    } else if (selected.getTeam().contains("DAL") && !t6blocked) {
                        team6++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numSF++;
                    }
                }
            }
            if (team1 == 3) {
                t1blocked = true;
            } else if (team2 == 3) {
                t2blocked = true;
            } else if (team3 == 3) {
                t3blocked = true;
            } else if (team4 == 3) {
                t4blocked = true;
            } else if (team5 == 3) {
                t5blocked = true;
            } else if (team6 == 3) {
                t6blocked = true;
            }
            if (numPF < 2 && totalSal <= 50000) {
                Player selected = listOfPlayers.getPlayerAt(i);
                if (selected.getPos().contains("PF") && selected.getSalary() <= 15000 && selected.getUsed() == false) {
                    if (selected.getTeam().contains("POR") && !t1blocked) {
                        team1++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPF++;
                    } else if (selected.getTeam().contains("OKC") && !t2blocked) {
                        team2++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPF++;
                    } else if (selected.getTeam().contains("WAS") && !t3blocked) {
                        team3++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPF++;
                    } else if (selected.getTeam().contains("PHO") && !t4blocked) {
                        team4++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPF++;
                    } else if (selected.getTeam().contains("LAL") && !t5blocked) {
                        team5++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPF++;
                    } else if (selected.getTeam().contains("DAL") && !t6blocked) {
                        team6++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numPF++;
                    }
                }
            }
            if (team1 == 3) {
                t1blocked = true;
            } else if (team2 == 3) {
                t2blocked = true;
            } else if (team3 == 3) {
                t3blocked = true;
            } else if (team4 == 3) {
                t4blocked = true;
            } else if (team5 == 3) {
                t5blocked = true;
            } else if (team6 == 3) {
                t6blocked = true;
            }
            if (numC < 1 && totalSal <= 60000) {
                Player selected = listOfPlayers.getPlayerAt(i);
                if (selected.getPos().contains("C") && selected.getSalary() <= 15000 && selected.getUsed() == false) {
                    if (selected.getTeam().contains("POR") && !t1blocked) {
                        team1++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numC++;
                    } else if (selected.getTeam().contains("OKC") && !t2blocked) {
                        team2++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numC++;
                    } else if (selected.getTeam().contains("WAS") && !t3blocked) {
                        team3++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numC++;
                    } else if (selected.getTeam().contains("PHO") && !t4blocked) {
                        team4++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numC++;
                    } else if (selected.getTeam().contains("LAL") && !t5blocked) {
                        team5++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numC++;
                    } else if (selected.getTeam().contains("DAL") && !t6blocked) {
                        team6++;
                        totalSal += selected.getSalary();
                        System.out.println("Selected the player:" + selected);
                        System.out.println("Added player. Total salary = $" + totalSal + "\n");
                        numC++;
                    }
                }
            }
            if (numRestarts == 10){
                break;
            }
            if (numPG < 2 && numSG < 2 && numC < 1 && numPF < 2 && numSF < 2 && (totalSal > 60000) || totalSal < 59000) {
                System.err.println("Restarting");
                numRestarts++;
                restart();
            }
        }
    }
    System.out.println("Number of PG: " + numPG);
    System.out.println("Number of PF: " + numPF);
    System.out.println("Number of SF: " + numSF);
    System.out.println("Number of SG: " + numSG);
    System.out.println("Number of C: " + numC);
    System.out.println("Members of POR: " + team1);
    System.out.println("Members of OKC: " + team2);
    System.out.println("Members of WAS: " + team3);
    System.out.println("Members of PHO: " + team4);
    System.out.println("Members of LAL: " + team5);
    System.out.println("Members of DAL: " + team6);
    System.out.println("Total Salary of lineup: $" + totalSal);
}                                              
public void restart() {
    team1 = 0;
    team2 = 0;
    team3 = 0;
    team4 = 0;
    team5 = 0;
    team6 = 0;
    numPG = 0;
    numPF = 0;
    numSF = 0;
    numSG = 0;
    numC = 0;
    t1blocked = false;
    t2blocked = false;
    t3blocked = false;
    t4blocked = false;
    t5blocked = false;
    t6blocked = false;
}
private int numRestarts = 0;
private int numC = 0;
private int numPF = 0;
private int numSF = 0;
private int numSG = 0;
private int numPG = 0;
private int numEntry = 0;
private int team1 = 0; //POR
private int team2 = 0; //OKC
private int team3 = 0; //WAS
private int team4 = 0; //PHO
private int team5 = 0; //LAL
private int team6 = 0; //DAL
private boolean t1blocked = false;
private boolean t2blocked = false;
private boolean t3blocked = false;
private boolean t4blocked = false;
private boolean t5blocked = false;
private boolean t6blocked = false;
inttotalsal=0;
而(numPG!=2&&nummsg!=2&&numC!=1&&numPF!=2&&numSF!=2&&totalSal<60000){//enquanto salario total<60000
对于(int i=0;i如果(NUMPG<2 &总数),这里有很多重复的代码,尤其是所有的<代码>系统.out .PrtLnf()。你应该考虑使用一些方法(如AuthoC> PrimtMeMeXAGE)(/Case>方法),以避免不必要的代码复制。我知道,将重新修改所有这些“Souts”。,我只使用它们来查看阶段,而不进行长达数小时的调试:\n好吧,当帖子包含344行代码,而您正在寻求帮助时,您可能希望首先修复这些代码,以帮助提高可读性,以便其他人可以帮助您。我可以这样做,但我最初打算在没有代码的情况下发布问题,因为我的问题不在代码,但实际上我对如何循环组合缺乏知识。有91c9种可能的组合,因此计算机应该能够在一分钟左右完成所有组合:\n我只是不知道过程背后的逻辑。顺便说一下,谢谢你的帮助!)