"""
用于将一个n*n的二维数组逆时针旋转45度后打印
"""
def rotateArr(arr):
lens = len(arr)
"打印二维数组右上部分"
i = lens - 1
while i > 0:
row = 0
col = i
while col < lens:
print(arr[row][col], end=' ')
row += 1
col += 1
print()
i -= 1
"打印二维数组左下部分,包括对角线"
i = 0
while i < lens:
row = i
col = 0
while row < lens:
print(arr[row][col], end=' ')
row += 1
col += 1
print()
i += 1
if __name__ == '__main__':
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
rotateArr(arr)
运行结果如下:
3
2 6
1 5 9
4 8
7