How could I combine several teams once and once?

I'm not sure how to make them play each other once and only once. For example, here is an example of commands (I will add the result later, looking for basic logical help):

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"; } } 
+4
source share
8 answers

I really spent some time and encoded the answer. This is a pretty funny question. There are many ways to solve this problem:

  • Fancy iteration
  • Recursion
  • Iteration on command appearance
  • Use separate structure for mark as processed

This image may help:

  0 1 2 3 0 - AAA 1 B - AA 2 BB - A 3 BBB - 

The X and Y axis of this matrix shows the answers you want. Either Set A or Set B will give you an answer. Note that you do not want the teams to play on their own, as shown by a bar in the matrix. Below are three uses for iteration:

 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)); } } } } 

Output:

Easy to read:

  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 

This is the value of 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 

This is the value of 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 
+3
source

Here is the basic algorithm

  • Take the first array command
  • so that this team plays every other array command
  • delete this command from the array or otherwise mark this command as already playing all so that you ignore it.
  • repeat
+5
source

Define two nested for loops, each of which corresponds to your commands. Typically, each team will play each team twice, and each team will play once.

You don’t want this, so initialize the looping variable for the inner for loop to one plus the current value of the loop variable for the outer for loop. This ensures that each combination of teams is repeated exactly once, and the team does not play itself.

Then call any game logic that is inside the inner for loop.

+1
source

A loop like this:

 for(int i = 0; i < aoTeams.length - 1; i++) { for(int j = i+1; j < aoTeams.length; j++) { aoTeams[i].playAgainst(aoTeams[j]); } } 
+1
source

Should be correct:

 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); 

Do not forget to use the first letter for classes in java first and always write fields in camelcase, starting with the lower letter; -)

+1
source

Maintain the java.util.BitSet link in your myTeams class and create it with a number equal to the number of commands. By default, all values ​​are not set (false) at the beginning.

As a team tries to play a match with another team in aoTeams , check the value in BitSet in the index of the opponent's array. If it is not installed, let it reproduce and set the value in this index. Do the same for the other team.

An example implementation might look like this:

 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); } } 

And here is how you use it:

 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); } } 
0
source

Create a Boolean array (say, already written) in the myTeams class, initialize them with all false in the constructor, when team1 plays with team2, set alreadyPassed [2] = true (in team1) and set alreadyPassed [1] = true in team2 .

0
source

Simple nested loop. Iterate through both lists and emit pairs, starting the second cycle with 1 + the first index of the cycle.

0
source

All Articles