L~Ljm 2021-11-28 15:12 采纳率: 0%
浏览 2

倒s型填数,求做法!()

img


就是给出n值,要求输出nxn的方阵,s型排列,从左上开始

  • 写回答

1条回答 默认 最新

  • 陈.py 2021-11-29 15:48
    关注
    
    import numpy as np
    
    n = int(input(""))
    Array = np.array(range(1, n * n + 1)).reshape(n, n)
    row = 0
    line = 0
    count1 = 1  # 计数器1用于计算蛇形矩阵填充的数
    Max = 2 * n - 2
    for total in range(n):
        if total % 2 == 0:
            for row in range(total + 1):
                line = total - row
                Array[row, line] = count1
                count1 = count1 + 1
        else:
            for line in range(total + 1):
                row = total - line
                Array[row, line] = count1
                count1 = count1 + 1
    
    count2 = 1  # 用于画下三角矩阵
    for total in range(n, Max + 1):
        if total % 2 == 0:
            for row in range(count2, n):
                line = total - row
                Array[row, line] = count1
                count1 = count1 + 1
            count2 = count2 + 1
        else:
            for line in range(count2, n):
                row = total - line
                Array[row, line] = count1
                count1 = count1 + 1
            count2 = count2 + 1
    for line in range(n):
        for row in range(n):
            if row + 1 != n:
                if Array[line, row] < 10:
                    print(' ', end='')
                print(Array[line, row], end='  ')
            else:
                if Array[line, row] < 10:
                    print(' ', end='')
                print(Array[line, row])
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 11月28日

悬赏问题

  • ¥15 List<Class>有参构造
  • ¥20 搭建三相栅极电路后高侧浮动地VS存在电容特性
  • ¥20 云卓h12pro 数传问题
  • ¥20 请问有人知道怎么用工艺库里面的sdb文件通过virtuoso导出来library里面每个cell的symbol吗?
  • ¥20 海思 nnie 编译 报错
  • ¥50 决策面并仿真,要求有仿真结果图
  • ¥15 springboot接入微信支付SDK
  • ¥50 大区域的遥感影像匹配 怎么做啊
  • ¥15 求解答:pytorch跑yolov8神经网络受挫
  • ¥20 Js代码报错问题不知道怎么解决