48 lines
1.2 KiB
C++
48 lines
1.2 KiB
C++
#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;
|
||
} |