Java 小例子:按照順時針或逆時針方向輸出一個數字矩陣__Java

來源:互聯網
上載者:User

題目:按照指定的長寬和輸出方向,從外向內列印一個從 1 開始的數字矩陣,矩陣的開始位置在左上方。如下圖

代碼及注釋如下:

public class NumberMatrix {    public static void main(String[] args) {        int width = 25;        int height = 12;        boolean clockwise = false;        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);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.