Round-robin tournament schedule: Difference between revisions

(promoted to full task)
Line 463:
1:11 12:10 2:9 3:8 4:7 5:6
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
 
public final class RoundRobinTournamentSchedule {
 
public static void main(String[] args) {
System.out.println("Round robin for 12 players:");
roundRobin(12);
System.out.println(System.lineSeparator());
System.out.println("Round robin for 5 players, 0 denotes a bye:");
roundRobin(5);
}
private static void roundRobin(int teamCount) {
if ( teamCount < 2 ) {
throw new IllegalArgumentException("Number of teams must be greater than 2: " + teamCount);
}
List<Integer> rotatingList = IntStream.rangeClosed(2, teamCount).boxed().collect(Collectors.toList());
if ( teamCount % 2 == 1 ) {
rotatingList.add(0);
teamCount += 1;
}
for ( int round = 1; round < teamCount; round++ ) {
System.out.print(String.format("%s%2d%s", "Round ", round, ":"));
List<Integer> fixedList = IntStream.rangeClosed(1, 1).boxed().collect(Collectors.toList());
fixedList.addAll(rotatingList);
for ( int i = 0; i < teamCount / 2; i++ ) {
System.out.print(String.format("%s%2d%s%2d%s",
" ( ", fixedList.get(i), " vs ", fixedList.get(teamCount - 1 - i), " )"));
}
System.out.println();
Collections.rotate(rotatingList, +1);
}
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
Round robin for 12 players:
Round 1: ( 1 vs 12 ) ( 2 vs 11 ) ( 3 vs 10 ) ( 4 vs 9 ) ( 5 vs 8 ) ( 6 vs 7 )
Round 2: ( 1 vs 11 ) ( 12 vs 10 ) ( 2 vs 9 ) ( 3 vs 8 ) ( 4 vs 7 ) ( 5 vs 6 )
Round 3: ( 1 vs 10 ) ( 11 vs 9 ) ( 12 vs 8 ) ( 2 vs 7 ) ( 3 vs 6 ) ( 4 vs 5 )
Round 4: ( 1 vs 9 ) ( 10 vs 8 ) ( 11 vs 7 ) ( 12 vs 6 ) ( 2 vs 5 ) ( 3 vs 4 )
Round 5: ( 1 vs 8 ) ( 9 vs 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