问题描述:
循环日程表问题。n=2^k个运动员进行网球循环赛,需要设计比赛日程表。每个选手必须与其他n−1个选手各赛一次;每个选手一天只能赛一次;循环赛一共进行n−1天。按此要求设计一张比赛日程表,该表有n行和n−1列,第i行j列为第i个选手第j天遇到的选手。
观察图表可以分为4个部分,用递归分治的方法填充每一部分。
#include <iostream>
using namespace std;
const int maxNum = 1 << 10;
int table[maxNum][maxNum];
void circulateSchedule(int row, int column, int n) {
// 递归出口
if(n == 1) {
return ;
}
// 将2^k*2^k的表格分成2^(k-1)*2^(k-1)的四个子表格
int half = n / 2;
// 每个表格的左上角赋值
// 左上子表格等于右下子表格,右上子表格等于左下子表格
// 右上子表格等于左上子表格加上子表格大小
table[row + half][column + half] = table[row][column];
table[row][column + half] = table[row + half][column] = table[row][column] + half;
// 递归四个子表格
circulateSchedule(row, column, half);
circulateSchedule(row, column + half, half);
circulateSchedule(row + half, column, half);
circulateSchedule(row + half, column + half, half);
}
int main() {
// 选手个数,n=2^k
int n;
while(true) {
cout << "请输入选手个数,0则退出程序:";
cin >> n;
if(!n) {
break;
}
// 初始化(0,0)点
table[0][0] = 1;
// 生成循环日程表
circulateSchedule(0, 0, n);
// 输出表
for(int i = 0; i < n; i++) {
for(int j = 1; j < n; j++) {
cout << table[i][j] << "\t";
}
cout << endl << endl << endl;
}
}
return 0;
}
原文:https://blog.csdn.net/q547550831/article/details/51545284