#C2195. Rotate Matrix 90 Degrees Clockwise
Rotate Matrix 90 Degrees Clockwise
Rotate Matrix 90 Degrees Clockwise
Given an n x n square matrix, your task is to rotate the matrix 90 degrees clockwise. The rotation can be described by the formula:
$$A_{i,j} = A'_{n-j+1,i}$$
where the element at row i and column j of the original matrix moves to row n-j+1 and column i in the rotated matrix. Implement a program that reads the square matrix from standard input and outputs the rotated matrix to standard output.
inputFormat
The input is given via standard input (stdin) in the following format:
n a11 a12 ... a1n a21 a22 ... a2n ... an1 an2 ... ann
Where n
is the size of the matrix and each of the next n
lines contains n
space-separated integers representing the matrix rows.
outputFormat
Output the rotated matrix via standard output (stdout) in the same format: n
lines with n
space-separated integers representing the rows of the rotated matrix.
3
1 2 3
4 5 6
7 8 9
7 4 1
8 5 2
9 6 3
</p>