本文實(shí)例講述了java實(shí)現(xiàn)的按照順時(shí)針或逆時(shí)針方向輸出一個(gè)數(shù)字矩陣功能。分享給大家供大家參考,具體如下:
題目:按照指定的長寬和輸出方向,從外向內(nèi)打印一個(gè)從 1 開始的數(shù)字矩陣,矩陣的開始位置在左上角。如下圖
代碼及注釋如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
public class numbermatrix { public static void main(string[] args) { int width = 25 ; int height = 12 ; boolean clockwise = false ; system.out.println( "服務(wù)器之家測試結(jié)果:" ); outputmatrix(width, height, clockwise); } /** * 按照指定的長寬和輸出方向,從外向內(nèi)打印一個(gè)從 1 開始的數(shù)字矩陣,矩陣的開始位置在左上角。 * * @param width 矩陣寬度 * @param height 矩陣高度 * @param clockwise 是否是順時(shí)針方向 */ private static void outputmatrix( int width, int height, boolean clockwise) { // 首先判斷最大數(shù)字的位數(shù),以決定輸出如何對齊 int numlength = ( int ) math.log10(width * height) + 1 ; // 決定輸出的格式(最大位數(shù) + 1個(gè)空格) string format = "%" + (numlength + 1 ) + "d" ; // 定義要輸出的二維數(shù)組,注意維度是從高到低的 // 此時(shí) matrix 中所有元素的值都是 0 int [][] matrix = new int [height][width]; // 定義一個(gè)位置指針和一個(gè)計(jì)數(shù)器,位置指針進(jìn)行移動(dòng),而計(jì)數(shù)器負(fù)責(zé)遞增,遞增后的數(shù)字 // 被填充進(jìn)矩陣,當(dāng) width * height 個(gè)數(shù)字填充完畢,這個(gè)矩陣就完成了。 // 注意這里位置指針的第一個(gè)元素對應(yīng) matrix 的第一個(gè)維度 y,第二個(gè)元素對應(yīng)第二個(gè)維度 x。 int [] pointer = { 0 , 0 }; int counter = 1 ; // 定義當(dāng)前移動(dòng)的方向:1、2、3、4 分別表示上、右、下、左。 // 順時(shí)針的起始方向?yàn)橛遥鏁r(shí)針的起始方向?yàn)橄隆?/code> int direction = clockwise ? 2 : 3 ; // 開始循環(huán)填充,每個(gè)填充分為三步 for ( int i = 1 , max = width * height; i <= max; i++) { // 1. 填充內(nèi)容 int y = pointer[ 0 ]; int x = pointer[ 1 ]; matrix[y][x] = counter; // 2. 計(jì)數(shù)器自增 counter += 1 ; // 3. 移動(dòng)到下一個(gè)位置,因?yàn)檫@地方比較復(fù)雜,所以開個(gè)方法實(shí)現(xiàn) direction = move(matrix, width, height, pointer, direction, clockwise); } // 矩陣填充完畢,按照正常的方式循環(huán)輸出即可 for ( int y = 0 ; y < height; y++) { for ( int x = 0 ; x < width; x++) { system.out.printf(format, matrix[y][x]); } system.out.println(); // 完成一行后輸出換行 } } /** * 在矩陣中移動(dòng) * * @param matrix 矩陣,用于判斷前進(jìn)方向的下一個(gè)位置是否已經(jīng)填充了數(shù)字,如果是則轉(zhuǎn)向 * @param width 矩陣的寬 * @param height 矩陣的高 * @param pointer 指針的當(dāng)前位置。調(diào)用本方法后里面的值會(huì)改變,除非方法返回 0 * @param direction 指針當(dāng)前移動(dòng)的方向 * @param clockwise 是否是要按順時(shí)針方向轉(zhuǎn)向 * * @return 移動(dòng)后的新方向(與原來的方向可能相同也可能不同)。如果無法再繼續(xù)移動(dòng),則返回 0 */ private static int move( int [][] matrix, int width, int height, int [] pointer, int direction, boolean clockwise) { // 先嘗試按照原來的方向移動(dòng)到 newpointer int [] newpointer = movedirectly(pointer, direction); // 檢查 newpointer 是否合法,如果合法則將其賦值給 pointer 并保持原來的方向,方法完成 if (isvalid(newpointer, matrix, width, height)) { system.arraycopy(newpointer, 0 , pointer, 0 , 2 ); return direction; } // 進(jìn)行轉(zhuǎn)向,重新從 pointer 朝新的方向移動(dòng) direction = turn(direction, clockwise); newpointer = movedirectly(pointer, direction); // 檢查 newpointer 是否合法(同前面一樣) if (isvalid(newpointer, matrix, width, height)) { system.arraycopy(newpointer, 0 , pointer, 0 , 2 ); return direction; } // 既無法前進(jìn)也無法轉(zhuǎn)向,那么無法繼續(xù)移動(dòng)。 return 0 ; } // 判斷矩陣中指定的位置是否可以填充 private static boolean isvalid( int [] newpointer, int [][] matrix, int width, int height) { // 位置不能超出矩陣范圍 if (newpointer[ 0 ] >= height || newpointer[ 0 ] < 0 || newpointer[ 1 ] >= width || newpointer[ 1 ] < 0 ) { return false ; } // 位置的內(nèi)容應(yīng)該為空 if (matrix[newpointer[ 0 ]][newpointer[ 1 ]] != 0 ) { return false ; } return true ; } // 轉(zhuǎn)向。根據(jù)我們對 direction 的定義,順時(shí)針就是 +1,逆時(shí)針就是 -1 private static int turn( int direction, boolean clockwise) { int newdirection = clockwise ? direction + 1 : direction - 1 ; if (newdirection > 4 ) { newdirection = 1 ; } else if (newdirection < 1 ) { newdirection = 4 ; } return newdirection; } /** * 朝指定的方向移動(dòng),并返回新的位置 * * @param pointer 當(dāng)前位置 * @param direction 方向 * * @return 新的位置 */ private static int [] movedirectly( int [] pointer, int direction) { int y = pointer[ 0 ]; int x = pointer[ 1 ]; switch (direction) { case 1 : return new int []{y - 1 , x}; case 2 : return new int []{y, x + 1 }; case 3 : return new int []{y + 1 , x}; case 4 : return new int []{y, x - 1 }; } throw new illegalargumentexception( "方向不正確: " + direction); } } |
運(yùn)行結(jié)果:
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
原文鏈接:http://blog.csdn.net/YidingHe/article/details/49924735