With n=2^k athletes to play a tennis round robin, now design a match schedule that meets the following requirements:
(1) Each contestant must compete with the other N-1 players;
(2) Each player can only race once a day;
(3) The round robin is carried out n-1 days altogether.
Fill in the table I and J of the tables with the contestants that I encountered on day J, as required by this request. The
divides all the players into two halves according to the divide-and-conquer strategy, and the game schedule of the N contestants can be determined by the game schedule designed for N/2 players. The game schedule is made simple by dividing the contestants recursively until only 2 players are left. Just let the 2 contestants play the game. The
algorithm is as follows:
The public class Test2_11 {/** * algorithm table assigns a round robin schedule to array A * @param k has n = 2^k athletes * @param A to store a round robin schedule and assign a value from subscript 1
At the beginning, subscript 0 is a null value.
*/public static void table (int k,int[][] a) {int n=1;
for (int i=1;i<=k;i++) n *= 2;
for (int i=1;i<=n;i++) a[i][1] = i; int m = 1; The subscript of the control array also indicates the spacing of the module when divided into a module for (int s=1;s<=k;s++) {n/= 2;
Wakahara N=8, the First division of the N/2=4 Group, the second 2 groups, the third 1 groups.
for (int t=1;t<=n;t++)//t represents the number of groups that are divided at a time, descending for (int i=m+1;i<=2*m;i++)//i∈[m+1,2*m]
for (int j=m+1;j<=2*m;j++) {//j∈[m+1,2*m]//The first copy of the diagonal unit element is one, the second is 4, the third is 8, and so on So the first time M=1 i,j∈[2,2], the second time m=2 i,j∈[3,4], the third m=4 when i,j∈[5,8] a[i+ (t-1) *2*m][j] = a[i+ (t-1) *2*m-m][j-m]
;
The group and group spacing is (t-1) *2*m elements a[i+ (t-1) *2*m-m][j] = a[i+ (t-1) *2*m][j-m], depending on the number of groups divided;
} m *= 2; }} PubLic static void Main (string[] args) {int n = 8;
with 8 athletes int k = 3; Int[][] A = new int[n+1][n+1];
0 rows and 0 columns are empty, and a value of table (K,A) is assigned from subscript 1;
for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) System.out.print (a[i][j]+ "");
System.out.println (); }
}
}
The results of the operation are as follows:
1 2 3 4 5 6 7 8 2 1 4 3 6 5 8
7
3 4 1 2 7 8 5 6
4 3 2 1 8 7 6 5
5 6 7 8 1 2 3
4 6 5 8 7 2 1 4 3 7 8 5 6 3 4 1 2
The round robin Calendar was printed, but the computer was printed in what order. As shown in the figure below, the first column is 0 because the transcription is from the second column, the order from small to large to indicate the order of printing .
0 2 (0) 1, 0 4 9 0 (3 0), and 6, 18, 20,
25.
0 5 (
0 8) (0 7 21 23 49 51 53 55).
Supplement : This algorithm divides groups by rows in fixed columns , and if you want to group rows by columns , simply change the 10th row to a[1][i]=i and the 19th row and 21 rows to:
a[i][j+ (t-1) *2*m] = a[i-m][j+ (t-1) *2*m-m];
a[i][j+ (t-1) *2*m-m] = a[i-m][j+ (t-1) *2*m];