forked from huihut/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatchTable.cpp
102 lines (84 loc) · 2.18 KB
/
MatchTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <math.h>
using namespace std;
// 循环赛日程安排函数声明
void MatchTable(int k, int n, int **table);
int main()
{
int n = 0, k = 0;
// 用户界面
cout << "---------------- 循环赛日程安排问题 ----------------" << endl;
cout << "请输入k(k>=0),构成 n=(2^k) 个选手的循环赛" << endl;
// 输入k值
cin >> k;
// 判断输入数据合法性,包括检查输入是否为数字,k值是否大于0
if (cin.fail() || k < 0)
{
cout << "输入k错误!" << endl;
system("pause");
return 0;
}
// 计算比赛日程表大小
n = pow(2, k);
// 分配日程表空间
int **table = new int *[n + 1];
for (int i = 0; i <= n; i++)
{
table[i] = new int[n + 1];
}
// 进行循环赛日程安排,生成日程表
MatchTable(k, n, table);
// 显示输出
cout << "------------------------------------------------" << endl;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout << table[i][j] << "\t";
}
cout << endl;
}
cout << "------------------------------------------------" << endl;
// 暂停查看结果
system("pause");
// 释放内存
for (int i = 0; i <= n; i++)
delete[] table[i];
delete[] table;
// 指针置空
table = NULL;
return 0;
}
// 进行循环赛日程安排,生成日程表
void MatchTable(int k, int n, int **table)
{
// 设置日程表第一行的值
for (int i = 1; i <= n; i++)
table[1][i] = i;
// 每次填充的起始填充位置
int begin = 1;
// 用分治法分separate份,循环求解
for (int separate = 1; separate <= k; separate++)
{
// 日程表进行划分
n /= 2;
// flag为每一小份的列的标记
for (int flag = 1; flag <= n; flag++)
{
// 操作行
for (int i = begin + 1; i <= 2 * begin; i++)
{
// 操作列
for (int j = begin + 1; j <= 2 * begin; j++)
{
// 把左上角的值赋给右下角
table[i][j + (flag - 1) * begin * 2] = table[i - begin][j + (flag - 1) * begin * 2 - begin];
// 把右上角的值赋给左下角
table[i][j + (flag - 1) * begin * 2 - begin] = table[i - begin][j + (flag - 1) * begin * 2];
}
}
}
// 进入日程表的下一个划分进行填充
begin *= 2;
}
}