蛇形矩阵

蛇形矩阵

http://www.nowcoder.com/questionTerminal/649b210ef44446e3b1cd1be6fa4cab5e

每次增加都是在原有的基础上增加。

图片说明

while True:
    try:
        size = int(input())
        base = [[0 for i in range(size)] for j in range(size)]
        count = 1
        for index in range(size):
            x = index
            for i in range(x, -1, -1):
                base[i][x-i] = count
                count += 1

        res = ''
        for i in base:
            for j in i:
                if j != 0:
                    res = res + str(j) + ' '
            print(res.strip())
            res = ''
    except Exception as e:
        print(e)
        break
5
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
全部评论
你好我用相似的方法为什么过不了 while True: try: import numpy as np a = int(input()) res = np.zeros([a,a], int) num = 1 for i in range(a): for j in range(i+1): res[i-j][j] = num num = num + 1 for lists in res: temp = [] for i in lists: if i != 0: temp.append(str(i)) print(" ".join(temp)) except: break
点赞 回复
分享
发布于 2020-01-15 14:24

相关推荐

4 收藏 评论
分享
牛客网
牛客企业服务