平方矩阵()

2023-12-23 18:00:52

平方矩阵1

平方矩阵2

曼哈顿距离?

#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

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

文章来源:https://blog.csdn.net/m0_63925226/article/details/135170650
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。