我不知道如何让他们一次只玩一次。例如,这里有一个团队样本(我稍后会添加评分,主要寻找逻辑帮助):
class MyTeams {
String teamName;
int wins;
int losses;
}
public class BasketallSeason {
public static void main(String[] args) {
MyTeams aoTeams[] = new MyTeams[9];
aoTeams[0].teamName = "Utah";
aoTeams[1].teamName = "USC";
aoTeams[2].teamName = "Saint Mary's";
aoTeams[3].teamName = "Oregon";
aoTeams[4].teamName = "San Diego";
aoTeams[5].teamName = "San Francisco";
aoTeams[6].teamName = "UCLA";
aoTeams[7].teamName = "Washington";
aoTeams[8].teamName = "Loyola";
}
}
答案 0 :(得分:5)
这是您的基本算法
答案 1 :(得分:3)
我实际上花了一些时间编写答案。这是一个非常有趣的问题。有许多方法可以解决这个问题:
这张照片可能有所帮助:
0 1 2 3
0 - A A A
1 B - A A
2 B B - A
3 B B B -
此矩阵的X和Y轴显示您想要的答案。 Set A或Set B都会给你答案。请注意,您不希望团队在矩阵中用短划线表示自己玩游戏。以下是使用迭代的3个选项:
public class BBall {
public static void main(String args[]) {
List<String> teams = new ArrayList<String>();
teams.add("Boston");
teams.add("LA");
teams.add("New York");
teams.add("Chicago");
teams.add("Dallas");
// This option might be a little easier to read.
int index1 = 0;
System.out.println("Easy to read:");
for (String team1 : teams) {
index1++;
for (int index2 = index1; index2 < teams.size(); ++index2) {
System.out.println(team1 + " plays " + teams.get(index2));
}
}
System.out.println("This is set A:");
for (int x = 1; x < teams.size(); x++) {
for (int y = x - 1; y >= 0; y--) {
System.out.println(teams.get(x) + " plays " + teams.get(y));
}
}
System.out.println("This is set B:");
for (int x = 0; x < teams.size() - 1; x++) {
for (int y = x + 1; y < teams.size(); y++) {
System.out.println(teams.get(x) + " plays " + teams.get(y));
}
}
}
}
输出:
易于阅读:
Boston plays LA
Boston plays New York
Boston plays Chicago
Boston plays Dallas
LA plays New York
LA plays Chicago
LA plays Dallas
New York plays Chicago
New York plays Dallas
Chicago plays Dallas
这是设置A:
LA plays Boston
New York plays LA
New York plays Boston
Chicago plays New York
Chicago plays LA
Chicago plays Boston
Dallas plays Chicago
Dallas plays New York
Dallas plays LA
Dallas plays Boston
这是设置B:
Boston plays LA
Boston plays New York
Boston plays Chicago
Boston plays Dallas
LA plays New York
LA plays Chicago
LA plays Dallas
New York plays Chicago
New York plays Dallas
Chicago plays Dallas
答案 2 :(得分:1)
定义两个嵌套for
循环,每个循环遍历您的团队。通常情况下,这会让每支球队每场比赛两次,每支球队都会打一次。
您不希望这样,因此将内部for
循环的循环变量初始化为加上外部for
循环的循环变量的当前值。这可以确保每个团队组合只迭代一次,团队不会自行发挥作用。
然后调用内部for
循环内的任何游戏逻辑。
答案 3 :(得分:1)
for(int i = 0; i < aoTeams.length - 1; i++) {
for(int j = i+1; j < aoTeams.length; j++) {
aoTeams[i].playAgainst(aoTeams[j]);
}
}
答案 4 :(得分:1)
应该是对的:
for (int i = aoTeams.length-1; i >= 0; i--)
for (int j = i-1; j >= 0; j--)
System.out.println(aoTeams[j].teamName + " : "+ aoTeams[i].teamName);
不要忘记首先使用大写字母表示java中的类,并且始终使用较低的字母开始在camelcase中写入字段; - )
答案 5 :(得分:0)
在班级myTeams
中维护java.util.BitSet
引用,并使用等于团队数量的数字对其进行实例化。默认情况下,所有值都在开头未设置(false)。
当一个团队尝试与aoTeams
中的另一个团队进行比赛时,检查对手阵列索引处BitSet
的值。如果没有设置则让它播放并设置该索引处的值。对其他团队也这样做。
示例实现可能如下所示:
class Team {
String teamName;
int wins;
int losses;
BitSet playRecord;
public Team(String name, int size) {
this.teamName = name;
playRecord = new BitSet(size);
}
public boolean hasPlayed(int index) {
return playRecord.get(index);
}
public void finishedPlaying(int index) {
playRecord.set(index);
}
}
这就是你如何使用它:
public static void main(String[] args) {
int size = 9;
Team aoTeams[] = new Team[size];
aoTeams[0] = new Team("Utah", size);
aoTeams[1] = new Team("USC", size);
play(aoTeams, 0, 1);
}
private static void play(Team[] teams, int indexA, int indexB) {
if (teams[indexA].hasPlayed(indexB)) {
// Teams have already played together
} else {
// Teams playing for the first time
teams[indexA].finishedPlaying(indexB);
teams[indexB].finishedPlaying(indexA);
}
}
答案 6 :(得分:0)
在myTeams类中创建一个布尔数组(比如havePlayed),在构造函数中使用所有false初始化它们,当team1使用team2时,设置alreadyPassed [2] = true(在team1的对象中)并设置alreadyPassed [1] = true在team2的对象中。
答案 7 :(得分:0)
一个简单的嵌套循环。迭代两个列表并发出对,在第一个循环索引的1+处开始第二个循环。