Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I actually spent some time and coded up the answer. It is a pretty fun question. There are <strong>many</strong> ways to solve this:</p> <ol> <li>Fancy iteration</li> <li>Recursion</li> <li>Iteration while popping off a team</li> <li>Use a separate structure to 'mark' teams as having been processed</li> </ol> <p>This picture might help:</p> <pre><code> 0 1 2 3 0 - A A A 1 B - A A 2 B B - A 3 B B B - </code></pre> <p>The X and Y axis of this matrix shows the answers that you want. Either Set A or Set B will give you the answer. Note that you don't want teams playing themselves as indicated by a dash in the matrix. Below are 3 options that use iteration:</p> <pre><code>public class BBall { public static void main(String args[]) { List&lt;String&gt; teams = new ArrayList&lt;String&gt;(); 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 &lt; teams.size(); ++index2) { System.out.println(team1 + " plays " + teams.get(index2)); } } System.out.println("This is set A:"); for (int x = 1; x &lt; teams.size(); x++) { for (int y = x - 1; y &gt;= 0; y--) { System.out.println(teams.get(x) + " plays " + teams.get(y)); } } System.out.println("This is set B:"); for (int x = 0; x &lt; teams.size() - 1; x++) { for (int y = x + 1; y &lt; teams.size(); y++) { System.out.println(teams.get(x) + " plays " + teams.get(y)); } } } } </code></pre> <p>The output:</p> <p>Easy to read:</p> <pre><code> 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 </code></pre> <p>This is set A:</p> <pre><code> 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 </code></pre> <p>This is set B:</p> <pre><code> 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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload