描述思路(2分)
请用你熟悉的语言编码显示(8分)
public static void main(String[] args) { int[][] arr = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; int n = 4; for(int i=0; i<n; i++){ for(int j = 0; j<=i; j++){ System.out.print(arr[j][j+n-i-1] + " "); } System.out.println(" "); } for(int i=n-2; i>=0; i--){ for(int j = 0; j<=i; j++){ System.out.print(arr[j+n-i-1][j] + " "); } System.out.println(" "); } }
public class PrintRotateMatrix { public static void main(String[] args) { int[][] arr = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; /** * 打印上半截 */ for (int i = 0, k = arr.length - 1; i < arr.length; i++, k--) { for (int j = 0; j < i + 1; j++) { System.out.print(arr[j][j + k] + " "); } System.out.println(); } /** * 打印下半截 */ for (int i = 0, k = 1; i < arr.length - 1; i++, k++) { for (int j = i + 1; j < arr.length; j++) { System.out.print(arr[j][j - k] + " "); } System.out.println(); } } }