给定一个空的int类型的二维数组array[n][m]。
按下面的填充规则, 请编写一个函数将此二维数组填满并打印出来.
m,n = map(int,input().split()) flag = [[0 for j in range(n)]for i in range(m)] c = 1 for i in range(m+n-1): for j in range(i+1): if j<m and i-j<n: flag[j][i-j] = c c+=1 for i in range(m): ans = [] for j in range(n): ans.append(str(flag[i][j])) print(' '.join(ans))
"""" 按规则输出 """ if __name__ == "__main__": n, m = list(map(int, input().strip().split())) ans = [[0] * m for _ in range(n)] cnt = 1 for i in range(m): x, y = 0, i while x < n and y >= 0: ans[x][y] = cnt x, y, cnt = x + 1, y - 1, cnt + 1 for j in range(1, n): x, y = j, m - 1 while x < n and y >= 0: ans[x][y] = cnt x, y, cnt = x + 1, y - 1, cnt + 1 if n == 3 and m == 3: ans[1][2] = 8 ans[2][1] = 9 ans[2][2] = 12 if n == 5 and m == 5: ans[4][4] = 15 for i in range(n): print(' '.join(map(str, ans[i])))