You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
//https://www.cnblogs.com/crx234/p/5988418.html
//日程表
int a[100][100];
int n; //选手的个数
/*
tox:目标数组的行号
toy:目标数组的列号
fromx:源数组的行号
fromy:源数组的列号
r:数组的大小为 r*r
这个函数本身没什么技术含量,就是一个方框内容拷贝的过程。关键在于调用时的参数传递。相当于一个辅助函数
*/
void Copy(int fromx, int fromy, int tox, int toy, int r) {
for (int i = 0; i < r; i++) //r*r这么大一块
for (int j = 0; j < r; j++)
a[tox + i][toy + j] = a[fromx + i][fromy + j];
}
void Print() {
//输出日程表
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
//分治法的核心函数
void Table(int k) {
n = 1 << k; // 1<<k表示的是2^k
//构造正方形表格的第一行数据,数据初始化
for (int i = 0; i < n; i++)
a[0][i] = i + 1;
//采用分治算法,构造整个循环赛日程表
for (int r = 1; r < n; r *= 2) //方框的边长肯定是2倍2倍增长,也就是复制的方框长度
//i是准备复制的方框起点纵坐标说白了就是从哪列开始行的话每次都是从第0行开始。
for (int i = 0; i < n; i += 2 * r) {
cout << "r=" << r << ",i=" << i << endl;
cout << "左上角复制到右下角: fromx=" << 0 << ",fromy=" << i << ",tox=" << r << ",toy=" << r + i << ", 方框大小:" << r
<< endl;
Copy(0, i, r, r + i, r); //左上角复制到右下角
Print();
cout << "右上角复制到左下角: fromx=" << 0 << ",fromy=" << r + i << ",tox=" << r << ",toy=" << i << ",方框大小:" << r
<< endl;
Copy(0, r + i, r, i, r); //右上角复制到左下角
Print();
cout << "====================================" << endl;
}
}
int main() {
int k;
cout << "请输入k的值";
cin >> k;
//计算日程表
Table(k);
//输出表格
Print();
return 0;
}