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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. /**
  2. * @Description 打印螺旋矩形
  3. *
  4. * @author Amos.young
  5. * @date 2015年11月17日
  6. */
  7. public class ArrayPrint {
  8.         private int N; // 矩形的维数
  9.         private int[][] array;

  10.         public ArrayPrint() {
  11.                 N = 5;
  12.                 array = new int[N][N];
  13.         }

  14.         public ArrayPrint(int n) {
  15.                 N = n;
  16.                 array = new int[N][N];
  17.         }

  18.         public void print() {
  19.                 int i = 0, j = 0, i0 = 0, j0 = 0, count = 1, num = N - 1;
  20.                 while (num > 0) {
  21.                         for (i = i0, j = j0; j < num; j++, count++) {
  22.                                 array[i][j] = count;
  23.                         }
  24.                         for (i = i0; i < num; i++, count++) {
  25.                                 array[i][j] = count;
  26.                         }
  27.                         for (j = num; j > i0; j--, count++) {
  28.                                 array[i][j] = count;
  29.                         }
  30.                         for (i = num; i > i0; i--, count++) {
  31.                                 array[i][j] = count;
  32.                         }
  33.                         i0++;
  34.                         j0++;
  35.                         num--;
  36.                 }
  37.                 if (N % 2 != 0) {
  38.                         array[N / 2][N / 2] = count;
  39.                 }
  40.                 for (i = 0; i < N; i++) {
  41.                         for (j = 0; j < N; j++) {
  42.                                 String ar = "0";
  43.                                 if (array[i][j] / 10 < 1)
  44.                                         ar = "\t" + array[i][j];
  45.                                 else if (array[i][j] / 100 < 1 && array[i][j] / 10 >= 1)
  46.                                         ar = "\t" + array[i][j];
  47.                                 System.out.print(ar + "\t");
  48.                         }
  49.                         System.out.println();
  50.                         System.out.println();
  51.                 }
  52.         }

  53.         public static void main(String[] args) throws IOException {
  54.                 int order = 0;
  55.                 System.out.println("Please input the order of the square:(< 10)");
  56.                 order = System.in.read();
  57.                 order = order - '0';
  58.                 ArrayPrint arrayPrint = new ArrayPrint(order);
  59.                 arrayPrint.print();
  60.         }
  61. }
复制代码

1 个回复

倒序浏览
  1. /**
  2. * @Description 打印等长的二维数组,要求从 1 开始的自然数由方阵的最外圈向内螺旋方式地顺序排序。
  3. *
  4. * @author Amos.young
  5. * @date 2015年10月28日
  6. */
  7. public class ArrayPrint {
  8.         /**
  9.          * 打印示例
  10.          *                 1         2         3          4
  11.          *                 12  13   14  5
  12.          *                 11  16   15  6
  13.          *                 10  9    8   7
  14.          *
  15.          * 思路
  16.          *         1.观察方阵的规律,每一圈左上角的数字是最小的设为 min,每一圈有 (n-1)*4 个数字,圈数设为 count。
  17.          *         2.可以做一个循环,给最外的一圈赋值,然后递归给里的一圈赋值,知道n=1或n=0为止
  18.          */
  19.         public static void main(String[] args) {
  20.                 int n = 4;
  21.                 int[][] arr = new int[n][n];
  22.                 int min = 1;
  23.                 int count = 0;
  24.                 setNum(n, arr, min, count);
  25.                 for (int i = 0; i < arr.length; i++) {
  26.                         for (int j = 0; j < arr.length; j++) {
  27.                                 System.out.print(arr[i][j] + "\t");
  28.                         }
  29.                         System.out.println();
  30.                 }
  31.         }

  32.         private static void setNum(int n, int[][] arr, int min, int count) {
  33.                 for (int i = 0; i <= (n - 1) * 4; i++) {
  34.                         if (n == 0) { // 循环结束,n为偶数的情况
  35.                                 break;
  36.                         }
  37.                         if (n == 1) { // 循环结束, n为奇数的情况
  38.                                 arr[count][count] = min;
  39.                                 break;
  40.                         }
  41.                         // n-1个为一组,一共赋值4组
  42.                         if (i < n - 1) { // 第一组,向右赋值
  43.                                 arr[count][count + i % (n - 1)] = min++;
  44.                         } else if (i < (n - 1) * 2) { // 第二组,向下赋值
  45.                                 arr[count + i % (n - 1)][count + n - 1] = min++;
  46.                         } else if (i < (n - 1) * 3) { // 第三组,向左赋值
  47.                                 arr[count + n - 1][count + n - 1 - i % (n - 1)] = min++;
  48.                         } else if (i < (n - 1) * 4) { // 第四组,向上赋值
  49.                                 arr[count + n - 1 - i % (n - 1)][count] = min++;
  50.                         } else {
  51.                                 // 当一圈赋值完毕,圈数加一,边长减二,递归调用
  52.                                 setNum(n - 2, arr, min, ++count);
  53.                         }
  54.                 }
  55.         }
  56. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马