add
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void table(int k, int **a, int n);
|
||||
void printTable(int **array, int n);
|
||||
|
||||
void table(int k, int **a, int n) {
|
||||
// 设置日程表第一行
|
||||
for (int i = 0; i < n; i++) {
|
||||
a[i][0] = i + 1; // 第一列设置为选手序号
|
||||
}
|
||||
// 填充表格
|
||||
for (int s = 0; s < k; s++) {
|
||||
int half = 1 << s; // 2^s
|
||||
for (int t = 0; t < (1 << (k - s - 1)); t++) { // 2^(k-s-1)
|
||||
for (int i = 0; i < half; i++) {
|
||||
for (int j = 0; j < half; j++) {
|
||||
a[i + half * t][j + half * t] = a[i][j];
|
||||
a[i + half * t][j + half * t + half] = a[i][j + half];
|
||||
a[i + half * t + half][j + half * t] = a[i + half][j];
|
||||
a[i + half * t + half][j + half * t + half] = a[i + half][j + half];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printTable(int **array, int n) {
|
||||
// 打印表头
|
||||
printf("选手 | ");
|
||||
for (int j = 1; j < n; j++) { printf("第%d天 | ", j); }
|
||||
printf("\n");
|
||||
// 打印分隔行
|
||||
for (int j = 0; j <= n; j++) { printf("-----"); }
|
||||
printf("\n");
|
||||
for (int i = 0; i < n; i++) {
|
||||
printf("%2d | ", array[i][0]); // 打印选手序号
|
||||
for (int j = 1; j < n; j++) { printf("%2d | ", array[i][j]); }
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int k = 3; // 2^3 = 8个运动员
|
||||
int n = 1 << k; // 计算2^k
|
||||
// 创建二维数组作为日程表
|
||||
int **array = (int **)malloc(n * sizeof(int *));
|
||||
for (int i = 0; i < n; i++) {
|
||||
array[i] = (int *)malloc(n * sizeof(int));
|
||||
for (int j = 0; j < n; j++) { // 确保初始化
|
||||
array[i][j] = 0;
|
||||
}
|
||||
}
|
||||
table(k, array, n);// 制作日程表
|
||||
printTable(array, n);// 输出日程表
|
||||
// 释放动态分配的内存
|
||||
for (int i = 0; i < n; i++) { free(array[i]); }
|
||||
free(array);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user