Files
python/TangDou/XiTi/1306_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

48 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
#define N 8
int a[101][101] = {0};
void table(int n) {
int i, j;
//这是n=2的时候完整的填充方法
if (n == 2) {
a[1][1] = 1;
a[1][2] = 2;
a[2][1] = 2;
a[2][2] = 1;
return;
}
//递归折半填充表
table(n / 2);
//递归结束后,其实左上角已经完成填充工作
//填左下角元素
int temp = n / 2;
for (i = temp + 1; i <= n; i++)
for (j = 1; j <= temp; j++)
a[i][j] = a[i - temp][j] + temp;
//填右上角元素
for (i = 1; i <= temp; i++)
for (j = temp + 1; j <= n; j++)
a[i][j] = a[i + temp][j - temp];
//填右下角元素
for (i = temp + 1; i <= n; i++)
for (j = temp + 1; j <= n; j++)
a[i][j] = a[i - temp][j - temp];
}
int main() {
int i, j;
//从8向2进行填充递归就是从大到小迭代就是从小到大
table(N);
//输出
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++)
cout << a[i][j] << "\t";
cout << endl;
}
return 0;
}