#K86162. Rotate Matrix Clockwise
Rotate Matrix Clockwise
Rotate Matrix Clockwise
In this problem, you are given an (n \times n) matrix. Your task is to rotate the matrix 90° clockwise in-place. This means you should modify the original matrix without using extra space for another matrix. The transformation can be concisely described by the following two steps:
- Transpose the matrix (i.e. swap (a_{ij}) and (a_{ji})).
- Reverse each row of the transposed matrix.
For example, consider the matrix:
[ \begin{bmatrix} 1 & 2 & 3 \ 4 & 5 & 6 \ 7 & 8 & 9 \end{bmatrix} ]
After rotating it 90° clockwise, the matrix becomes:
[ \begin{bmatrix} 7 & 4 & 1 \ 8 & 5 & 2 \ 9 & 6 & 3 \end{bmatrix} ]
Write a program that reads the matrix from standard input, performs the rotation, and outputs the resulting matrix to standard output.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains a single integer (n), representing the number of rows (and columns) in the matrix.
- Each of the next (n) lines contains (n) integers separated by spaces, representing a row of the matrix.
outputFormat
Print the rotated matrix to standard output (stdout). The output should consist of (n) lines, each containing (n) space-separated integers. The matrix should be rotated 90° clockwise relative to the input matrix.## sample
3
1 2 3
4 5 6
7 8 9
7 4 1
8 5 2
9 6 3
</p>