a015. 矩陣的翻轉
行和列的調換,[i][j] → [j][i],用巢狀for迴圈接受輸入,顛倒行列之後存進matrix中,最後照順序輸出matrix。
程式碼如下:
- /* a015:矩陣的翻轉
- *
- * 2020/2/27
- *
- */
- import java.util.Scanner;
- public class Pa015{
- public static void main(String [] args){
- Scanner scanner = new Scanner(System.in);
- while(scanner.hasNext()){
- int row = scanner.nextInt();
- int col = scanner.nextInt();
- int[][] matrix = new int[col][row];
- for(int i = 0; i < row; i++){
- for(int j = 0; j < col; j++){
- matrix[j][i] = scanner.nextInt();
- }
- }
- for(int i = 0; i < col; i++){
- for(int j = 0; j < row; j++){
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
- }
沒有留言:
張貼留言