Reference Blog: http://blog.csdn.net/liufeng_king/article/details/8488421
Problem Description:
with n = 2^k athletes want to play a tennis round robin. A match schedule is now designed to meet the following requirements:
(1) Each contestant must match with other n-1 players once;
(2) Each contestant can only race once a day;
(3) The round robin is carried out n-1 days altogether.
I would like to write the analysis, how to write no other people's blog analysis of the comprehensive, so direct reference to facilitate future review
Please follow this request to design the tournament schedule as a table with n rows and n-1 columns. In row I of the table, column J is filled with the contestants who were encountered by the first player on the J Day. Which 1≤i≤n,1≤j≤n-1. The game schedule for the 8 contestants is as follows:
algorithm idea : According to the Divide and conquer strategy, we can divide all the players into two halves, then the game schedule of N contestants can be decided by the N/2 player's game schedule. By recursively dividing the contestants with this two-in-one strategy, the game schedule is made simple until there are only two players left. Just let the two contestants play the game. As shown above, the table of squares listed is the game schedule for 8 contestants. The upper left corner and the lower left corner of the two small pieces for the player 1 to 4 and 5 to the player 8 the first 3 days of the game schedule. Accordingly, all the numbers in the upper left corner are copied to the lower right corner by their relative position, and all the numbers in the lower left corner are copied to the upper right corner according to their relative position, so that we have arranged the contestants 1 to 4 and 5 to 8 for the last 4 days of the game schedule. In this way, it is easy to extend the game schedule to a situation with any number of players.
algorithm Steps :
(1) Output the first line of the schedule with a For loop for (int i=1;i<=n;i++) A[1][i] = i
(2) then define an m value, m initialized to 1,m to control the starting fill position of I (I for rows) and J (j for columns) for each time the fill table is filled.
(3) Use a For loop to divide the problem into several parts, for k=3,n=8, the problem is divided into 3 parts, the first part, according to the first line has been filled, fill the second line, the second part, according to the first part has been filled, fill the 34th line, the third part, according to already filled the top four rows, Fill in the last four lines. for (ints=1;s<=k;s++) n/=2;
(4) Divide each part mentioned in ③ with A For loop for (intt=1;t<=n;t++) for the first part, divide it into four small units, i.e. divide the second line as follows
Similarly, the second part (that is, three or four lines), divided into two parts, the third part of the same. (5) Finally, according to the above for Loop on the whole division and the idea of dividing the method, each cell is filled. The fill principle is: diagonal fill
for (int i=m+1;i<=2*m;i++)//i control line
for (int j=m+1;j<=2*m;j++)//j control column
{
a[i][j+ (t-1) *m*2]= a[i-m][j+ (t-1) *m*2-m];/* the lower-right value equals the value in the upper-left corner */
a[i][j+ (t-1) *m*2-m] =a[i-m][j+ (t-1) *m*2];/* the value in the lower-left corner equals the value in the upper-right corner */}
Run the process :
(1) Fill the second row with the first line initialized
(2) completed by the first part of S control. Then the s++, the second part of the fill
(3) Finally, the third part of the filling
The code is as follows:
#include <stdio.h>
#include <string.h>
int a[1000][1000];
void output (int k);
int Table (int k)
{
int i,j;
int n=1;
for (i=1;i<=k;i++)
n*=2;
for (i=1;i<=n;i++)
a[1][i]=i;
int m=1;
for (int s=1;s<=k;s++)
{
n/=2;
for (int t=1;t<=n;t++)
{for
(i = m+1; I <= 2*m; i++)
{for
(j = m+1; J <=2*m; j + +)
{
a[i][j+ (t-1) *m*2] = a[i-m][j+ (t-1) *m*2-m];
a[i][j+ (t-1) *m*2-m] = a[i-m][j+ (t-1) *m*2];
}} m*=2;
}
Output (k);
}
void output (int k)
{
int n=1,i,j;
for (i=1;i<=k;i++)
n*=2;
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
printf ("%d", a[i][j]);
Putchar (' \ n ');}
}
int main ()
{
int i,j,k;
scanf ("%d", &k);
Table (k);
return 0;
}