1. Problem description:
Picture rotation is one of the easiest ways to deal with pictures. In this question, you need to rotate the picture 90 degrees clockwise. We use an n×m two-dimensional array to represent a picture. For example, an example of a 3×4 picture is given below:
1 3 5 7
9 8 7 6
3 5 9 7
The picture after this picture is rotated 90 degrees clockwise is as follows:
3 9 1
5 8 3
9 7 5
7 6 7
Given the initial picture, please calculate the rotated picture
[Input format]
The first line of input contains two integers n and m, which represent the number of rows and columns, respectively. The next n lines, m integers in each line, represent a given picture. Each element (pixel) in the image is an integer with a value between 0 and 255 (including 0 and 255).
[Output format]
Output m rows and n columns, representing the rotated picture.
2. Thinking analysis:
Analyzing the question, we can know that we need to store the data input from the console first. You can use r, c = map(int, input().split()) to get the number of rows and columns of the console input matrix, and then process it in a loop For each line of input data, you can use the map function to convert the input number to int type: list(map(int, input().split())) gets a line of int type data, add each line of data to The final obtained in a list is a two-dimensional list, and finally the value in the matrix is output according to the position relationship between the rotated matrix and the original matrix.
3. The code is as follows:
if __name__ == "__main__":
r, c = map(int, input().split())
matrix = list()
for i in range(r):
# 保存输入数据, map函数将输入的数据转为int类型
matrix.append(list(map(int, input().split())))
for i in range(c):
# 按照逆序的方式输出
for j in range(r - 1, -1, -1):
print(matrix[j][i], end=" ")
print()