怪物头子丶 2021-11-11 11:40 采纳率: 50%
浏览 90
已结题

急!求解一道java的面试题。


package demo;

public class Xunhuan {

    public static void main(String[] args) {

    /*
    在start方法内控制台打印输入以下内容

    01 02 03 04 05 06 07 08 09 10
    28 29 30 31 32 33 34 35 36 11
    27 48 49 50 51 52 53 54 37 12
    26 47 60 59 58 57 56 55 38 13
    25 46 45 44 43 42 41 40 39 14
    24 23 22 21 20 19 18 17 16 15

     */
        start(9, 8);
    }

    /**
     * @param row    行数
     * @param column 列数
     */
    private static void start(int row, int column) {


    }

}
  • 写回答

2条回答 默认 最新

  • 其樂无穷 2021-11-11 13:12
    关注
        private void start(int row, int column) {
            final int[][] map = new int[row][column];
            int y = 0, x = 0, fillNum = 0, direction = 0;
            do {
                if (map[y][x] == 0) {
                    map[y][x] = ++fillNum;
                    continue;
                }
                while (true) {
                    int newY = y + ((direction == 3) ? -1 : (direction & 1));
                    int newX = x + ((direction == 2) ? -1 : (direction & 1) ^ 1);
                    if (newX < column && newX >= 0 && newY >= 0 && newY < row && map[newY][newX] == 0) {
                        y = newY;
                        x = newX;
                        break;
                    }
                    direction = (direction + 1) & 3;
    
                }
            } while (fillNum < column * row);
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < column; j++) {
                    System.out.printf("%02d ", map[i][j]);
                }
                System.out.println();
            }
    
        }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月11日
  • 已采纳回答 11月11日
  • 创建了问题 11月11日