Schedule of the tennis round robin (Division strategy)-with detailed code

Source: Internet
Author: User

I. Problem description: N athletes are requiredTennis round robin. Design a calendar meeting the following requirements:
(1) Each contestant must compete with the other n-1 contestants each time;
(2) Each contestant can only compete once a day;
(3) 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.
Ii. Problem Analysis andAlgorithmDesign:
According to the grouping policy, all contestant pairs can be divided into two groups (if n is an even number, it is directly divided into n/2, and if n is an odd number, it is (n + 1) /2 in each group), the schedule of n players can be determined by the schedule designed for (n/2 or (n + 1)/2) players. Recursively split the contestants using this split strategy until only two players are left, the preparation of the competition schedule becomes very simple. In this case, you only need to let the two players compete.
The following table lists the schedules of the six contestants. The first column indicates 1-6 contestants, and the second column to the sixth column indicates the contestants from the first day to the fifth day.
1 2 3 4 5 6
2 1 5 3 6 4
3 6 1 2 4 5
4 5 6 1 3 2
5 4 2 6 1 3
6 3 4 5 2 1
HereAlgorithmThe difficulty of the design is the merger after separate governance. Here I will describe it in combination with the example of the above six contestants. First, divide the six contestants into two peer groups, each of which has three players. Then, recursively divide the three contestants into two peer groups, with two players in each group.
In the case of two players, the two players competed. The following table lists the schedules of the two contestants:
1 2
2 1
The next task is to merge the schedules of the two contestants in the two groups to get the schedules of the three contestants. Here I assume that there are four contestants participating in the competition:
1 2
2 1
3 4
4 3
In the next game, let's have 1 and 3 matches, 2 and 4 matches, and 1 and 4 matches, 2 and 3 matches on the third day, that is, let the players in the previous group, the following table shows the schedule of the round robin and the last group of contestants:
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
Here we will get a schedule for three contestants. Then, if 4th contestants use 0 as the hypothetical contestant, we will get a schedule for the three contestants:
1 2 3 0
2 1 0 3
3 0 1 2
The next task is to merge the schedules of the two three contestants to get the schedules of the six contestants. Here, the first three days of the competition between the two contestants are as follows:

1 2 3 0
2 1 0 3
3 0 1 2
4 5 6 0
5 4 0 6
6 0 4 5
Both contestant 3 and contestant 6 have no opponents on the first day, so they can play two games. Contestant 2 and contestant 5 have no opponents on the second day, so they can play two games ,; on the third day, Contestant 1 and contestant 4 had no opponents and asked them to compete. In this way, we can get the schedule of the first three days of the six contestants after the merger:
1 2 3 4
2 1 5 3
3 6 1 2
4 5 6 1
5 4 2 6
6 3 4 5
List the contestants in the two groups that match each other in the previous three days:
1 2 3
4 5 6
Here we can see that three and six, two, five, one, and four of the two groups have been compared. Here we will skip these contestant competitions, and then the two groups will play in a loop:
1 2 3
5 6 4
And
1 2 3
6 4 5
In this way, the complete schedule of the six contestants is obtained:
1 2 3 4 5 6
2 1 5 3 6 4
3 6 1 2 4 5
4 5 6 1 3 2
5 4 2 6 1 3
6 3 4 5 2 1
Iii. ProofAlgorithmCorrectness:
(1) At n = 2, the two contestants are playing for only one day.AlgorithmInitial Condition,AlgorithmYes.
(2) At n = K, if K is an even number, K players are divided into k/2 two groups, so that according to the problem requirements K players a total of K-1 days, if K/2 players are even numbers, then the game (K/2)-one day, during the merger, the two groups of K/2 players will need K/two days for the loop competition, then the first group and then merge a total of (K/2)-1 + (K/2) = K-1 days; k/2 players if the odd number, then the game K/2 days, during the merger, each contestant in the two groups had a match, so it would take (K/2)-1 day for the two K/2 contestants to go round, then grouping first and then merging a total of (K/2) + (K/2)-1 = K-1 days.
(3) the case where K is an odd number is similar to that where K is an even number.

Reprinted Website: http://www.stubc.com/thread-2645-1-1.html

Code:
  1. /***
  2. ** The following functions are from the Wang Xiaodong algorithm book, but they are not explained and cannot be understood at all. You can find the above algorithm analysis on the Internet.
  3. I drew a picture on the paper and finally figured out what was going on. I sorted out the code in the book and implemented it on vc6.0.
  4. Post it to share with you
  5.  
  6. **
  7. ***/
  8. # Include <iostream>
  9. Using namespace STD;
  10. Const int size = 50;
  11. Int A [size] [size];
  12. Void copy (int n );
  13. Void tournament (int n );
  14. Bool odd (int n );
  15. Void makecopy (int n );
  16. Void copyodd (int n );
  17. Int main ()
  18. {
  19. Int N;
  20. Int I, J;
  21. Cin> N;
  22. Tournament (N );
  23. If (ODD (N) // if n is an odd number or an even number, consider
  24. {
  25. For (I = 1; I <= N; I ++)
  26. {
  27. For (j = 1; j <= n + 1; j ++)
  28. If (A [I] [J] = n + 1)
  29. Cout <"0 ";
  30. Else
  31. Cout <A [I] [J] <"";
  32. Cout <Endl;
  33. }
  34. }
  35. Else
  36. {
  37. For (I = 1; I <= N; I ++)
  38. {
  39. For (j = 1; j <= N; j ++)
  40. Cout <A [I] [J] <"";
  41. Cout <Endl;
  42. }
  43. }
  44. Return 0;
  45. }
  46. /**
  47. * COPY Copies all the numbers in the small block calculated recursively in the upper left corner to the lower right corner.
  48. Add n/2 to all the numbers in the upper-right corner and copy them to the lower-left corner.
  49. Copy all numbers to the top-right corner *
  50. **/
  51. Void copy (int n)
  52. {
  53. Int M = n/2;
  54. For (INT I = 1; I <= m; I ++)
  55. For (Int J = 1; j <= m; j ++)
  56. {
  57. A [I] [J + M] = A [I] [J] + m;
  58. A [I + M] [J] = A [I] [J + M];
  59. A [I + M] [J + M] = A [I] [J];
  60. }
  61. }
  62. Void tournament (int n)
  63. {
  64. If (n = 1)
  65. {
  66. A [1] [1] = 1;
  67. Return;
  68. }
  69. If (ODD (n ))
  70. {
  71. Tournament (n + 1 );
  72. Return;
  73. }
  74. Tournament (n/2 );
  75. Makecopy (N );
  76. }
  77. Bool odd (int n)
  78. {
  79. Return N & 1;
  80. }
  81. Void makecopy (int n) // makecopy is similar to the copy algorithm, but n/2 is an odd or even number.
  82. {
  83. If (n/2> 1 & odd (n/2 ))
  84. Copyodd (N );
  85. Else
  86. Copy (N );
  87. }
  88. Void copyodd (int n) // implement copy when n/2 is an odd number
  89. {
  90. Int B [size];
  91. Int M = n/2;
  92. For (INT I = 1; I <= m; I ++)
  93. {
  94. B [I] = m + I;
  95. B [M + I] = B [I];
  96. }
  97. For (I = 1; I <= m; I ++) // This loop is hard to understand. Try to walk n = 6 on the paper, you can understand why you want to write this code.
  98. {
  99. For (Int J = 1; j <= m + 1; j ++)
  100. {
  101. If (A [I] [J]> m)
  102. {
  103. A [I] [J] = B [I];
  104. A [M + I] [J] = (B [I] + M) % N;
  105. }
  106. Else
  107. A [M + I] [J] = A [I] [J] + m;
  108. }
  109. For (j = 2; j <= m; j ++)
  110. {
  111. A [I] [M + J] = B [I + J-1];
  112. A [B [I + J-1] [M + J] = I;
  113. }
  114. }
  115. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.