#K83552. Matrix Operations: Transpose and Multiplication
Matrix Operations: Transpose and Multiplication
Matrix Operations: Transpose and Multiplication
You are given an operation command and one or two matrices. Your task is to perform the following operations:
- T: Compute the transpose of the given matrix.
- M: Multiply the first matrix by the second matrix. Note that the number of columns of the first matrix must equal the number of rows of the second matrix.
Input is read from standard input and the result (the resulting matrix) must be printed to standard output with each row on a new line and elements separated by spaces.
inputFormat
The first line contains a single character representing the operation: either T
(for transpose) or M
(for multiplication).
If the operation is T
:
- The next line contains two integers n and m, indicating the number of rows and columns of the matrix.
- This is followed by n lines, each containing m space-separated integers.
If the operation is M
:
- The next line contains two integers n and m for the first matrix, followed by n lines each with m space-separated integers.
- The subsequent line contains two integers r and c for the second matrix (with the guarantee that m = r), followed by r lines each with c space-separated integers.
outputFormat
Output the resulting matrix. For a transpose operation, the output will have m lines (each with n integers). For a multiplication operation, the output will have n lines (each with c integers). Each row should be printed on a separate line with space-separated values.
## sampleT
3 3
1 2 3
4 5 6
7 8 9
1 4 7
2 5 8
3 6 9
</p>