平方矩阵()

平方矩阵1

平方矩阵2

曼哈顿距离

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
相关推荐
大怪v1 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工3 小时前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农4 小时前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了4 小时前
AcWing学习——双指针算法
c++·算法
moonlifesudo5 小时前
322:零钱兑换(三种方法)
算法
NAGNIP1 天前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队1 天前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja1 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶1 天前
算法 --- 字符串
算法