1,數組順時針旋轉90度
1 2 //數組順時針旋轉90° 3 //列變行(正 0列-->0行,1列-->1行...) 4 //行變列(反 0行-->length-1列,1行-->length-2列...) 5 public class Rotate { 6 public static void getRotate(int[][] x) { 7 8 int[][] b = new int[x[0].length][x.length]; 9 // 轉換 10 for (int i = 0; i < x.length; i++) { 11 for (int j = 0; j < x[i].length; j++) { 12 b[j][x.length - i - 1] = x[i][j]; 13 } 14 } 15 // 輸出 16 for (int i = 0; i < b.length; i++) { 17 for (int j = 0; j < b[i].length; j++) { 18 System.out.print(b[i][j] + " "); 19 } 20 System.out.println(); 21 } 22 23 } 24 // 測試 25 public static void main(String[] args) { 26 int[][] a = {{ 0, 1 ,2 }, 27 { 0, 1 ,5 }, 28 { 1, 1 ,6 }, 29 { 1, 2 ,5 }, 30 { 1, 2 ,5 } 31 }; 32 getRotate(a); 33 } 34 }
運行結果:
1 1 1 0 0
2 2 1 1 1
5 5 6 5 2
2,九九乘法表
1 /** 8、九九乘法表 */ 2 public class Job8 { 3 4 public static void main(String[] args) { 5 for (int i = 1; i <= 9; i++) { 6 for (int j = 1; j <= i; j++) { 7 System.out.print(j + "*" + i + "=" + i * j + "\t"); 8 } 9 System.out.println(); 10 } 11 } 12 }
運行結果: