2020年2月27日 星期四

[zerojudge]a015. 矩陣的翻轉

a015. 矩陣的翻轉

行和列的調換,[i][j] → [j][i],用巢狀for迴圈接受輸入,顛倒行列之後存進matrix中,最後照順序輸出matrix。

程式碼如下:

  1. /* a015:矩陣的翻轉
  2. *
  3. * 2020/2/27
  4. *
  5. */
  6.  
  7. import java.util.Scanner;
  8.  
  9. public class Pa015{
  10.  
  11. public static void main(String [] args){
  12. Scanner scanner = new Scanner(System.in);
  13.  
  14. while(scanner.hasNext()){
  15. int row = scanner.nextInt();
  16. int col = scanner.nextInt();
  17.  
  18. int[][] matrix = new int[col][row];
  19.  
  20. for(int i = 0; i < row; i++){
  21. for(int j = 0; j < col; j++){
  22. matrix[j][i] = scanner.nextInt();
  23. }
  24. }
  25.  
  26. for(int i = 0; i < col; i++){
  27. for(int j = 0; j < row; j++){
  28. System.out.print(matrix[i][j] + " ");
  29. }
  30. System.out.println();
  31. }
  32.  
  33. }
  34. }
  35.  
  36. }

沒有留言:

張貼留言