A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© adalvik 中级黑马   /  2015-4-14 18:55  /  559 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

刚才在论坛转悠看到一个朋友在问输出输出n=5的螺旋矩阵(好像以前书上说的是蛇形打印),以前刚好研究了一下 就把代码贴出来了

  1. class Demo {
  2.         static int length = 5;
  3.         static int value = 1;
  4.         static int[][] snake = new int[length][length];
  5.         static Direction lastDirection = Direction.Right;

  6.         static enum Direction {
  7.                 Right, Down, Left, Up;
  8.         }

  9.         public static void initialArray() {
  10.                 int row = 0, col = 0;
  11.                 for (int c = 0; c < length * length; c++) {
  12.                         snake[row][col] = value;
  13.                         lastDirection = findDirection(row, col);
  14.                         switch (lastDirection) {
  15.                         case Right:
  16.                                 col++;
  17.                                 break;
  18.                         case Down:
  19.                                 row++;
  20.                                 break;
  21.                         case Left:
  22.                                 col--;
  23.                                 break;
  24.                         case Up:
  25.                                 row--;
  26.                                 break;
  27.                         default:
  28.                                 System.out.println("error");
  29.                         }
  30.                         value++;
  31.                 }
  32.         }

  33.         static Direction findDirection(int row, int col) {
  34.                 Direction direction = lastDirection;
  35.                 switch (direction) {
  36.                 case Right: {
  37.                         if ((col == length - 1) || (snake[row][col + 1] != 0))
  38.                                 direction = direction.Down;
  39.                         break;
  40.                 }
  41.                 case Down: {
  42.                         if ((row == length - 1) || (snake[row + 1][col] != 0))
  43.                                 direction = direction.Left;
  44.                         break;
  45.                 }
  46.                 case Left: {
  47.                         if ((col == 0) || (snake[row][col - 1] != 0))
  48.                                 direction = direction.Up;
  49.                         break;
  50.                 }
  51.                 case Up: {
  52.                         if (snake[row - 1][col] != 0)
  53.                                 direction = direction.Right;
  54.                         break;
  55.                 }
  56.                 }
  57.                 return direction;
  58.         }

  59.         public static void main(String[] args) {
  60.                 initialArray();

  61.                 for (int i = 0; i < length; i++) {
  62.                         for (int j = 0; j < length; j++) {
  63.                                 System.out.printf(" %2d", snake[i][j]);
  64.                         }
  65.                         System.out.println();
  66.                 }
  67.         }
  68. }
复制代码
输出结果

  1  2  3  4  5
16 17 18 19  6
15 24 25 20  7
14 23 22 21  8
13 12 11 10  9



评分

参与人数 2技术分 +1 黑马币 +20 收起 理由
println + 20 很给力!
lwj123 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马