曼哈顿距离
cpp
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 110;
int n;
int a[N][N];
int main()
{
while(cin >> n, n)
{
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
{
if (n % 2)
a[i][j] = (n + 1) / 2 - max(abs(i - n / 2), abs(j - n / 2));
else
a[i][j] = (n - 1) / 2.0 - max(abs((n - 1) / 2.0- i), abs((n - 1) / 2.0 - j)) + 1;
}
for (int i = 0; i < n; i ++ )
{
for (int j = 0; j < n; j ++ )
cout << a[i][j] << ' ';
cout << endl;
}
cout << endl;
}
return 0;
}
2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2
python
while True:
n = int(input())
if not(n): break
for i in range(n):
for j in range(n):
print(min(j, n - j - 1, i, n - i - 1) + 1, end = ' ')
print()
print()
1
1 1
1 1
1 1 1
1 2 1
1 1 1
1 1 1 1
1 2 2 1
1 2 2 1
1 1 1 1
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1