算法導論:打印迴旋數

  問題:打印輸出逆時針螺旋矩陣,要求螺旋矩陣的階數由用戶輸入。例如 n=4時,輸出的螺旋矩陣以下:算法

  

  下面給出個人代碼:  數組

 1 package org.warnier.zhang.exercises;
 2 
 3 public class Convolution {
 4     private int n;
 5     private int[][] array;
 6     private int number = 1;
 7 
 8     public Convolution(int n) {
 9         this.n = n;
10         array = new int[n][n];
11         init();
12     }
13 
14     private void init() {
15         for (int i = 0; i < n; i++) {
16             for (int j = 0; j < n; j++) {
17                 array[i][j] = 0;
18             }
19         }
20     }
21 
22     //逆時針方向迴旋;
23     public void generateConvolutionNumber() {
24         //處理迭代「內圈」數字輸出;
25         int a = 0;
26         int b = n -1 - a;
27         while (b >= a) {
28             //與正常的數組輸出方向一致螺旋輸出;
29             for (int i = a; i <= b; i++) {
30                 for (int j = a; j <= b; j++) {
31                     if (i != b && j == a) {
32                         array[i][j] = number++;
33                     }
34                     if (i == b && j != b) {
35                         array[i][j] = number++;
36                     }
37                 }
38             }
39             
40             //與正常的數組輸出方向相反螺旋輸出;
41             for (int i = b; i >= a; i--) {
42                 for (int j = b; j >= a; j--) {
43                     if (i != a && j == b) {
44                         array[i][j] = number++;
45                     }
46                     if (i == a && j != a) {
47                         array[i][j] = number++;
48                     }
49                 }
50             }
51             
52             a ++;
53             b = n -1 - a;
54         }
55         
56         //打個補丁:沒能解決n爲奇數的狀況!(沒有加上這句代碼,中間的空格輸出默認值0!)
57         if(n % 2 != 0){
58             array[n/2][n/2] = number;
59         }
60     }
61 
62     public void vprintf() {
63         for (int i = 0; i < n; i++) {
64             for (int j = 0; j < n; j++) {
65                 System.out.printf("%3d", array[i][j]);
66             }
67             System.out.println();
68         }
69     }
70 
71 }

  簡要說明,和 的存在,是爲了解決螺旋輸出內圈的數字;下面的是測試代碼:測試

 1 package org.warnier.zhang.exercises;
 2 
 3 public class ConvolutionTest {
 4 
 5     public static void main(String[] args) {
 6         Convolution convolution = new Convolution(5);
 7         convolution.generateConvolutionNumber();
 8         convolution.vprintf();
 9     }
10 
11 }
12 
13 結果:
14   1 16 15 14 13
15   2 17 24 23 12
16   3 18 25 22 11
17   4 19 20 21 10
18   5 6  7  8  9

  這是個人解決方法,主要是將回旋劃分爲從上到下,從左到右,從下到上,從右到左的四個方向分別處理,要注意後兩個方向與正常的數組輸出方向相反。這道算法題對學生黨來講有點壓力,可是掌握了可以寫出好多好玩的東西,好比貪吃蛇。上面的算法只是一種可能的實現,歡迎讀者貼上本身的代碼,交流交流!this

  根據讀者回復,能夠經過初始化將全部的數組元素賦值爲 n * n 的方式,解決當 n 爲奇數時,螺旋輸出爲默認值0的問題,下面貼上代碼:spa

 1 package org.warnier.zhang.exercises;
 2 
 3 public class ConvolutionPro {
 4     private int n;
 5     private int[][] array;
 6     private int number = 1;
 7 
 8     public ConvolutionPro(int n) {
 9         this.n = n;
10         array = new int[n][n];
11         init();
12     }
13 
14     private void init() {
15         for (int i = 0; i < n; i++) {
16             for (int j = 0; j < n; j++) {
17                 //使用初始化賦值解決n爲奇數的狀況!
18                 array[i][j] = n * n;
19             }
20         }
21     }
22 
23     //逆時針方向迴旋;
24     public void generateConvolutionNumber() {
25         //處理迭代「內圈」數字輸出;
26         int a = 0;
27         int b = n -1 - a;
28         while (b >= a) {
29             //與正常的數組輸出方向一致螺旋輸出;
30             for (int i = a; i <= b; i++) {
31                 for (int j = a; j <= b; j++) {
32                     if (i != b && j == a) {
33                         array[i][j] = number++;
34                     }
35                     if (i == b && j != b) {
36                         array[i][j] = number++;
37                     }
38                 }
39             }
40             
41             //與正常的數組輸出方向相反螺旋輸出;
42             for (int i = b; i >= a; i--) {
43                 for (int j = b; j >= a; j--) {
44                     if (i != a && j == b) {
45                         array[i][j] = number++;
46                     }
47                     if (i == a && j != a) {
48                         array[i][j] = number++;
49                     }
50                 }
51             }
52             
53             a ++;
54             b = n -1 - a;
55         }
56     }
57 
58     public void vprintf() {
59         for (int i = 0; i < n; i++) {
60             for (int j = 0; j < n; j++) {
61                 System.out.printf("%3d", array[i][j]);
62             }
63             System.out.println();
64         }
65     }
66 
67 }

  固然,咱們天然想到也能夠順時針螺旋輸出,相應地調整上面兩個for()的執行順序便可。 3d

相關文章
相關標籤/搜索