蛇形数组

问题描述:

  用户输入一个数n,根据n输出蛇形数组:例如用户输入n=5:

  

要求:

  时间限制:1秒 空间限制:32768K 热度指数:398594

解决方案:

  类似贪吃蛇游戏,只不过这里规定了方向。既然有方向,肯定要做标记,再定几个特殊点:左上角,右上角,左下角,右下角。

代码(Java):

public class Test {
    private static String[][] insert(int n) {
        int count = 1;
        int row = -1;// 行的最小值,为了计算方便这里取值为-1,如果取值为0,计算就复杂一些
        int col = 0;// 列的最小值
        int currentX = 0;// 当前的行坐标
        int currentY = 0;// 当前的列坐标
        String[][] array = new String[n][n];
        for (int i = 0; i < n; i++) {
            if (currentY == 0 && currentX == 0) {
                row++;
                for (int j = 0; j < n; j++) {
                    if (row < n && array[row][col] == null) {
                        if (count < 10) {
                            array[row++][col] = "0" + count++;
                        } else {
                            array[row++][col] = "" + count++;
                        }
                    }
                    currentX++;
                }
                row--;
            }
            if (currentY == 0 && currentX == n) {
                col++;
                for (int j = 0; j < n; j++) {
                    if (col < n && array[row][col] == null) {
                        if (count < 10) {
                            array[row][col++] = "0" + count++;
                        } else {
                            array[row][col++] = "" + count++;
                        }
                    }
                    currentY++;
                }
                col--;
            }
            if (currentY == n && currentX == n) {
                row--;
                for (int j = n - 1; j >= 0; j--) {
                    if (row >= 0 && array[row][col] == null) {
                        if (count < 10) {
                            array[row--][col] = "0" + count++;
                        } else {
                            array[row--][col] = "" + count++;
                        }
                    }
                    currentX--;
                }
                row++;
            }
            if (currentY == n && currentX == 0) {
                col--;
                for (int j = n - 1; j >= 0; j--) {
                    if (col >= 0 && array[row][col] == null) {
                        if (count < 10) {
                            array[row][col--] = "0" + count++;
                        } else {
                            array[row][col--] = "" + count++;
                        }
                    }
                    currentY--;
                }
                col++;
            }
        }
        return array;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[][] result = insert(n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }
    }
}

 

posted @ 2019-10-26 16:25  小留情  阅读(142)  评论(0)    收藏  举报