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

  1. import java.util.*;

  2. public class Tmp
  3. {
  4.         public static int[][] getScrewMatrix_1(int n)
  5.         {
  6.                 int[][] a = new int[n][n];
  7.                 int x = 1;
  8.                 int i = 0;
  9.                 int j = 0;
  10.                 int d = 0;
  11.                
  12.                 while (x <= n * n)
  13.                 {
  14.                         switch(d)
  15.                         {
  16.                         case 0:
  17.                                 a[i][j++] = x++;
  18.                                 if (j == n || a[i][j] != 0)
  19.                                 {
  20.                                         --j;
  21.                                         ++i;
  22.                                         d = (d + 1) % 4;
  23.                                 }
  24.                                 break;
  25.                         case 1:
  26.                                 a[i++][j] = x++;
  27.                                 if (i == n || a[i][j] != 0)
  28.                                 {
  29.                                         --i;
  30.                                         --j;
  31.                                         d = (d + 1) % 4;
  32.                                 }
  33.                                 break;
  34.                         case 2:
  35.                                 a[i][j--] = x++;
  36.                                 if (j == -1 || a[i][j] != 0)
  37.                                 {
  38.                                         ++j;
  39.                                         --i;
  40.                                         d = (d + 1) % 4;
  41.                                 }
  42.                                 break;
  43.                         case 3:
  44.                                 a[i--][j] = x++;
  45.                                 if (i == -1 || a[i][j] != 0)
  46.                                 {
  47.                                         ++i;
  48.                                         ++j;
  49.                                         d = (d + 1) % 4;
  50.                                 }
  51.                                 break;
  52.                         }
  53.                 }
  54.                
  55.                 return a;
  56.         }
  57.        
  58.         public static int[][] getScrewMatrix_2(int n)
  59.         {
  60.                 int[][] a = new int[n][n];
  61.                 int x = 0;
  62.                
  63.                 for (int i = 0, j = 0; i < (n + 1) / 2; ++i, ++j)
  64.                 {
  65.                         while (j < n - 1 - i)
  66.                                 a[i][j++] = ++x;
  67.                         while (i < j)
  68.                                 a[i++][j] = ++x;
  69.                         while (j > n - 1 - i)
  70.                                 a[i][j--] = ++x;
  71.                         while (i > j)
  72.                                 a[i--][j] = ++x;
  73.                 }
  74.                 if (n % 2 == 1)
  75.                         a[n / 2][n / 2] = ++x;
  76.                
  77.                 return a;
  78.         }
  79.        
  80.         public static void printScrewMatrix(int[][] a)
  81.         {
  82.                 int bit = getNumLength(a.length * a.length) + 1;
  83.                
  84.                 for (int i = 0; i < a.length; i++)
  85.                 {
  86.                         for (int j = 0; j < a[i].length; j++)
  87.                         {
  88.                                 for (int k = 1; k <= bit - getNumLength(a[i][j]); k++)
  89.                                         System.out.print(" ");
  90.                                 System.out.print(a[i][j]);
  91.                         }
  92.                         System.out.println();
  93.                 }
  94.         }
  95.        
  96.         public static int getNumLength(int n)
  97.         {
  98.                 int i = 0;
  99.                
  100.                 do
  101.                 {
  102.                         ++i;
  103.                         n /= 10;
  104.                 } while (n != 0);
  105.                
  106.                 return i;
  107.         }
  108.        
  109.         public static void main(String[] args)
  110.         {
  111.                 Scanner sc = new Scanner(System.in);
  112.                 printScrewMatrix(getScrewMatrix_1(sc.nextInt()));
  113.                 printScrewMatrix(getScrewMatrix_2(sc.nextInt()));
  114.                 sc.close();
  115.         }
  116. }
复制代码


2 个回复

倒序浏览
从运行效率角度,两种方式是一样的。
从代码简洁角度,显然第二种方式更胜一筹。
回复 使用道具 举报
受益匪浅。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马