设有n(n = 2^k)位选手参加网球循环赛,循环赛共进行n-1天,每位选手要与其他n-1位选手比赛一场,且每位选手每天必须比赛一场,不能轮空。试按此要求为比赛安排日程:
- 每个选手必须与其他n-1个选手各赛一场;
- 每个选手一天只能赛一场;
- 循环赛一共进行n-1天。
idea: Divide and conquer
源码如下:
#include <stdio.h> #include <stdlib.h> #include<assert.h> //定义了宏assert #define MAXSIZE 100 void mainlineCopy(int A[][MAXSIZE],int playerNum,int row,int column){ int i,j; if (playerNum==1) return ; mainlineCopy(A,playerNum/2,row,column); mainlineCopy(A,playerNum/2,row,column+playerNum/2); for(i=row;i<row+playerNum/2;i++) for(j=column;j<column+playerNum/2;j++) A[i+playerNum/2][j+playerNum/2]=A[i][j] ; for(i=row;i<row+playerNum/2;i++) for(j=column;j<column+playerNum/2;j++) A[i+playerNum/2][j]=A[i][j+playerNum/2] ; } int main (){ int A[MAXSIZE][MAXSIZE]={0,0,0,0}; int index; int playerNum=1; int i,j; printf("Please input the valve of K:"); scanf("%d",&index); assert(index>0&&index<=5); //不在0到5之间时 显示错误信息 并终止程序运行 for( j=1;j<=index;j++) playerNum*=2; for( i=0;i<playerNum;i++) A[1][i]=i+1; mainlineCopy(A,playerNum,1,0); for(i=1;i<=playerNum;i++){ for(j=0;j<playerNum;j++) printf("%2d ",A[i][j]); printf("\n"); } }