华为OD机考-数字螺旋矩阵(JAVA 2025B卷)

发布于:2025-06-13 ⋅ 阅读:(11) ⋅ 点赞:(0)

在这里插入图片描述
在这里插入图片描述

public class RotateMatrix {
    public static void main(String[] args) {
        // 顺时针螺旋矩阵
        printMatrixV1();
        // 逆时针螺旋矩阵
        //printMatrixV2();

    }

    private static void printMatrixV2() {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextLine()){
            String[] line = scan.nextLine().split(" ");
            int number = Integer.parseInt(line[0]);
            int row = Integer.parseInt(line[1]);
            int column = (number + row -1)/row;//向上取整
            String[][] matrix = new String[row][column];
            int k = 0;
            int left = 0,right = column - 1,top = 0,bottom=row-1;
            while(left <= right && top <= bottom){
                // 从上到下
                for(int i=top;i<=bottom;i++){
                    matrix[i][left] = ++k>number?"*":""+k;
                }
                left++;
                // 从左到右
                for(int i=left;i<=right;i++){
                    matrix[bottom][i] = ++k>number?"*":""+k;
                }
                bottom--;
                // 从下到上
                if(left<=right){
                    for(int i=bottom;i>=top;i--){
                        matrix[i][right] = ++k>number?"*":""+k;
                    }
                    right--;
                }

                // 从右到左
                if(top<=bottom){
                    for(int i=right;i>=left;i--){
                        matrix[top][i] = ++k>number?"*":""+k;
                    }
                    top++;
                }
            }

            for(int i=0;i<row;i++){
                for(int j=0;j<column;j++){
                    System.out.print(matrix[i][j]+" ");
                }
                System.out.println();
            }
        }

    }

    private static void printMatrixV1() {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextLine()){
            String[] line = scan.nextLine().split(" ");
            int number = Integer.parseInt(line[0]);
            int row = Integer.parseInt(line[1]);
            int column = (number + row -1)/row;//向上取整
            String[][] matrix = new String[row][column];
            int k = 0;
            int left = 0,right = column - 1,top = 0,bottom=row-1;
            while(left <= right && top <= bottom){
                // 从左到右
                for(int i = left; i<=right; i++){
                    matrix[top][i] = ++k>number?"*":""+k;
                }
                top++;
                // 从上到下
                for(int i=top;i<=bottom;i++){
                    matrix[i][right]= ++k>number?"*":""+k;
                }
                right--;
                // 从右到左
                if(top<=bottom){//防止重复赋值
                    for(int i=right;i>=left;i--){
                        matrix[bottom][i]=++k>number?"*":""+k;
                    }
                    bottom--;
                }

                // 从下到上
                if(left<=right){//防止重复赋值
                    for(int i=bottom;i>=top;i--){
                        matrix[i][left]=++k>number?"*":""+k;
                    }
                    left++;
                }
            }

            for(int i=0;i<row;i++){
                for(int j=0;j<column;j++){
                    System.out.print(matrix[i][j]+" ");
                }
                System.out.println();
            }
        }
    }


}