本文實例講述了Java實現的按照順時針或逆時針方向輸出一個數字矩陣功能。分享給大家供大家參考,具體如下:
題目:按照指定的長寬和輸出方向,從外向內打印一個從1 開始的數字矩陣,矩陣的開始位置在左上角。如下圖
代碼及註釋如下:
public class NumberMatrix { public static void main(String[] args) { int width = 25; int height = 12; boolean clockwise = false; System.out.println("武林網測試結果:"); outputMatrix(width, height, clockwise); } /** * 按照指定的長寬和輸出方向,從外向內打印一個從1 開始的數字矩陣,矩陣的開始位置在左上角。 * * @param width 矩陣寬度* @param height 矩陣高度* @param clockwise 是否是順時針方向*/ private static void outputMatrix(int width, int height, boolean clockwise) { // 首先判斷最大數字的位數,以決定輸出如何對齊int numLength = (int) Math.log10(width * height) + 1; // 決定輸出的格式(最大位數+ 1個空格) String format = "%" + (numLength + 1) + "d"; // 定義要輸出的二維數組,注意維度是從高到低的// 此時matrix 中所有元素的值都是0 int[][] matrix = new int[height][width]; // 定義一個位置指針和一個計數器,位置指針進行移動,而計數器負責遞增,遞增後的數字// 被填充進矩陣,當width * height 個數字填充完畢,這個矩陣就完成了。 // 注意這裡位置指針的第一個元素對應matrix 的第一個維度y,第二個元素對應第二個維度x。 int[] pointer = {0, 0}; int counter = 1; // 定義當前移動的方向:1、2、3、4 分別表示上、右、下、左。 // 順時針的起始方向為右,逆時針的起始方向為下。 int direction = clockwise ? 2 : 3; // 開始循環填充,每個填充分為三步for (int i = 1, max = width * height; i <= max; i++) { // 1. 填充內容int y = pointer[0]; int x = pointer[1]; matrix[y][x] = counter; // 2. 計數器自增counter += 1; // 3. 移動到下一個位置,因為這地方比較複雜,所以開個方法實現direction = move(matrix, width, height, pointer, direction, clockwise); } // 矩陣填充完畢,按照正常的方式循環輸出即可for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { System.out.printf(format, matrix[y][x]); } System.out.println(); // 完成一行後輸出換行} } /** * 在矩陣中移動* * @param matrix 矩陣,用於判斷前進方向的下一個位置是否已經填充了數字,如果是則轉向* @param width 矩陣的寬* @param height 矩陣的高* @param pointer 指針的當前位置。調用本方法後裡面的值會改變,除非方法返回0 * @param direction 指針當前移動的方向* @param clockwise 是否是要按順時針方向轉向* * @return 移動後的新方向(與原來的方向可能相同也可能不同)。如果無法再繼續移動,則返回0 */ private static int move(int[][] matrix, int width, int height, int[] pointer, int direction, boolean clockwise) { // 先嘗試按照原來的方向移動到newPointer int[] newPointer = moveDirectly(pointer, direction); // 檢查newPointer 是否合法,如果合法則將其賦值給pointer 並保持原來的方向,方法完成if (isValid(newPointer, matrix, width, height)) { System.arraycopy(newPointer, 0, pointer, 0, 2); return direction; } // 進行轉向,重新從pointer 朝新的方向移動direction = turn(direction, clockwise); newPointer = moveDirectly(pointer, direction); // 檢查newPointer 是否合法(同前面一樣) if (isValid(newPointer, matrix, width, height)) { System.arraycopy(newPointer, 0, pointer, 0, 2); return direction; } // 既無法前進也無法轉向,那麼無法繼續移動。 return 0; } // 判斷矩陣中指定的位置是否可以填充private static boolean isValid(int[] newPointer, int[][] matrix, int width, int height) { // 位置不能超出矩陣範圍if (newPointer[0] >= height || newPointer[0] < 0 || newPointer[1] >= width || newPointer[1] < 0) { return false; } // 位置的內容應該為空if (matrix[newPointer[0]][newPointer[1]] != 0) { return false; } return true; } // 轉向。根據我們對direction 的定義,順時針就是+1,逆時針就是-1 private static int turn(int direction, boolean clockwise) { int newDirection = clockwise ? direction + 1 : direction - 1; if (newDirection > 4) { newDirection = 1; } else if (newDirection < 1) { newDirection = 4; } return newDirection; } /** * 朝指定的方向移動,並返回新的位置* * @param pointer 當前位置* @param direction 方向* * @return 新的位置*/ private static int[] moveDirectly(int[] pointer, int direction) { int y = pointer[0]; int x = pointer[1]; switch (direction) { case 1: return new int[]{y - 1, x}; case 2: return new int[]{y, x + 1}; case 3: return new int[]{y + 1, x}; case 4: return new int[]{y, x - 1}; } throw new IllegalArgumentException("方向不正確: " + direction); }}運行結果:
更多關於java算法相關內容感興趣的讀者可查看本站專題:《Java數據結構與算法教程》、《Java操作DOM節點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。