题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
咱比较笨,不会找数学规律,说下思路:
1、创建n*n矩阵,-1赋值
2、蛇形的规律是,直接模拟。
for i in range(n): for k in range(i+1): matrix[i-k][k] = num
3、按行输出结果
import sys n = int(input()) matrix = [[-1]*n for _ in range(n)] num = 1 for i in range(n): for k in range(i+1): matrix[i-k][k] = num num += 1 for i in range(n): print(' '.join([str(k) for k in matrix[i] if k != -1]))