In the past few days, I have watched an itat competition with a question like this.
1. There are n teams to carry out a volleyball Round Robin and a schedule meeting the following requirements is designed:
A) each team must compete with the other n-1 teams once;
B) each team can only play once a day;
C) WHEN n is an even number, the round robin starts for n-1 days. When N is an odd number, the round robin lasts for N days.
Example of a match schedule with n = 6 (numbers 6 teams from 1 to 6 ):
Match schedule for n = 6
|
Day 1 |
Day 2 |
Day 3 |
Day 4 |
Day 5 |
|
1 ~ 2 |
1 ~ 3 |
1 ~ 4 |
1 ~ 5 |
1 ~ 6 |
|
3 ~ 5 |
2 ~ 4 |
2 ~ 5 |
2 ~ 6 |
2 ~ 3 |
|
4 ~ 6 |
5 ~ 6 |
3 ~ 6 |
3 ~ 4 |
4 ~ 5 |
N = 5 example of the competition schedule (add number 0, when 0 is hit, the round is blank ):
Match schedule n = 5
|
Day 1 |
Day 2 |
Day 3 |
Day 4 |
Day 5 |
|
1 ~ 0 |
1 ~ 5 |
1 ~ 4 |
1 ~ 3 |
1 ~ 2 |
|
2 ~ 5 |
0 ~ 4 |
5 ~ 3 |
4 ~ 2 |
3 ~ 0 |
|
3 ~ 4 |
2 ~ 3 |
0 ~ 2 |
5 ~ 0 |
4 ~ 5 |
After thinking for a long time, I also read a lot of references about partition algorithms. (Not quite clear)
Later, someone posted a post with a great idea!
Bytes ------------------------------------------------------------------------------------------------
Take the 6 teams as an example and use numbers to form a ring.
Team 1 does not move, and each round turns counter-clockwise, so that teams can skip from the head of Team 1 in turn. The current round of match is obtained from each shift.
No matter how many teams have met the requirements.
Use an array to process the specific implementation.
---
1 4
2 5
3 6
---
1 5
4 6
2 3
---
1 6
5 3
4 2
---
1 3
6 2
5 4
----
1 2
3 4
6 5
----------------------------------------------
Provided by ahyyxx222
It's actually quite simple.
I used a consumer list implementation.
Public class t {private int num; // number of teams private partition list <integer> List = new partition list <integer> (); Public T (int n) {This. num = N; Init () ;}private void Init () {If (Num % 2 = 0) // an even number of teams {for (INT I = 0; I <num; I ++) {list. add (I + 1) ;}} else // an odd number of teams {for (INT I = 0; I <num; I ++) {list. add (I + 1);} List. add (0) ;}} public void print () {for (INT I = 0; I <list. size ()-1; I ++) {system. out. println ("day" + (I + 1) + "day"); For (Int J = 0; j <list. size ()/2; j ++) {system. out. println (list. get (j) + "--" + list. get (list. size ()-1-j);} int temp = List. polllast (); // move the list. add (1, temp );}}