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.

44 lines
1.3 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;
//目标:理解递归,思考怎样把问题分解为更小的同样的问题
int A[1000][1000];
int i, j;
//很显示这是一个递归的函数,要深入理解递归
void solve(int n) {
if (n == 1) {
A[0][0] = 1;//递归边界,标记好第一个元素
} else {
int m = n / 2;//划分为四块后,每块的边长为原来的一半
//求解左上角
solve(m);
for (i = m; i < n; i++)//左下角可由左上角对应的每个数加边长得到
for (j = 0; j < m; j++)
A[i][j] = A[i - m][j] + m;
for (i = 0; i < m; i++)//右上角可由左下角复制而得到
for (j = m; j < n; j++)
A[i][j] = A[i + m][j - m];
for (i = m; i < n; i++)//右下角可由左上角复制而得到
for (j = m; j < n; j++)
A[i][j] = A[i - m][j - m];
}
}
int main() {
int k;
cin >> k;
//求解边长为n2的k次幂的循环日程表
int n = 1 << k;
//主函数调用
solve(n);
//输出结果
for (int i = 0; i < n; i++, printf("\n"))
for (int j = 0; j < n; j++)
printf("%3d", A[i][j]);
return 0;
}