#K10616. Matrix Rotation: 90-degree Clockwise Operation
Matrix Rotation: 90-degree Clockwise Operation
Matrix Rotation: 90-degree Clockwise Operation
Given an N x N matrix, your task is to rotate the matrix by 90 degrees clockwise.
The rotation should be achieved by reading the matrix from stdin
and writing the rotated matrix to stdout
. Each row of the matrix is separated by a newline, and each element in a row is separated by a single space.
Formally, if the input matrix is denoted as \( M \) of size \( N \times N \), the rotated result \( M' \) is defined such that:
\[ M'_{ij} = M_{(N-1-j)\,i} \]
It is guaranteed that the matrix is always square. Your solution should handle matrices of varying sizes, including the minimum size of 1x1 and matrices containing negative numbers.
inputFormat
The first line of input contains a single integer \( N \) representing the size of the square matrix. The following \( N \) lines each contain \( N \) space-separated integers, representing the rows of the matrix.
Example:
3 1 2 3 4 5 6 7 8 9
outputFormat
Output the rotated matrix in the same format as the input: \( N \) lines, where each line contains \( N \) space-separated integers corresponding to a row of the rotated matrix.
Example (corresponding to the input example):
7 4 1 8 5 2 9 6 3## sample
3
1 2 3
4 5 6
7 8 9
7 4 1
8 5 2
9 6 3
</p>