This article is original. If you need to reprint it, please indicate the author and the source. Thank you!
In the description Algorithm First, take a look at the following 5*5 Table:
| 1 |
3 |
4 |
10 |
11 |
| 2 |
5 |
9 |
12 |
19 |
| 6 |
8 |
13 |
18 |
20 |
| 7 |
14 |
17 |
21 |
24 |
| 15 |
16 |
22 |
23 |
25 |
The above table shows the rule easily. Is to start from the first lattice in the upper left corner (starting from 1), and then extend the diagonal line from the upper right corner to the lower left corner. First from bottom to top, then from top to bottom. Start to increment by number. That is to say, each diagonal line has the following numbers:
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
Because it is first from top to bottom (1 can be seen as top to bottom), then from bottom to top, it is like a round-trip in a sports competition, therefore, this digital table can also be called a round-trip digital table. Now we want to work with a method (or function). The method parameter is of the int type, meaning n. The method returns a two-dimensional array, indicating the round-trip relay numeric table to be obtained.
In fact, this algorithm is not complex. You only need to obtain the coordinates of the Two-dimensional array corresponding to each number in 1 to n ^ 2 respectively. Take the table with five rows and five columns as an example and obtain the coordinates of each array (starting from 0 ).
0th groups 1st groups 2nd groups 3rd groups 4th groups 5th groups 6th groups 7th groups 8th groups |
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 |
(0, 0) (1, 0) (0, 1) ) (3, 0) (2, 1) (1, 2) (0, 3) ) ) (2, 4) (3, 3) (4, 2) (4,3) (3,4) (4, 4) |
From the above, we can see a rule. The abscissa and ordinate of the half table in the upper left corner (divided by diagonal lines) Start from 0, and each group increases by 1 until it increases to the boundary of the table (n-1, that is to say, an even row is an index of column addition, row reduction, and row + column = group. While the four groups of numbers in the lower right corner of the table grow alternately, the decreasing rows or columns always start from (n-1) (in this example, it starts from 4 ), the incremental rows or columns always start from index-n + 1, where index indicates the index of the group. This gives us an algorithm. Implementation Code (Java version: Public Static Int [] [] Getgrid ( Int N)
{
Int [] [] Array = New Int [N] [N];
Int Row = 0 , Col = 0 , M = 1 ;
// Used to control parity groups. "false" indicates an even group, and "true" indicates an odd group.
Boolean Isrow = False ;
// I indicates the index of the current group, starting from 0
For ( Int I = 0 ; I < ( 2 * N - 1 ); I ++ )
{
Row = I;
While (Row > = (I < N) ? 0 : I - N + 1 ))
{
// If the number in the table in the lower right corner is processed, the maximum number of rows or columns cannot exceed n-1
If (Row > (N - 1 ))
Row = N - 1 ;
Col = I - Row;
If (Isrow)
Array [row] [col] = M;
Else // Convert row to column and Col to row
Array [col] [row] = M;
M ++ ;
Row -- ;
}
// Switch the parity Group
Isrow = ! Isrow;
}
Return Array;
}
If you want to output a number table with n = 10, you can use int [] [] grid = getgrid (10); to output this grid and see if it is the following result:
| 1 |
3 |
4 |
10 |
11 |
21 |
22 |
36 |
37 |
55 |
| 2 |
5 |
9 |
12 |
20 |
23 |
35 |
38 |
54 |
56 |
| 6 |
8 |
13 |
19 |
24 |
34 |
39 |
53 |
57 |
72 |
| 7 |
14 |
18 |
25 |
33 |
40 |
52 |
58 |
71 |
73 |
| 15 |
17 |
26 |
32 |
41 |
51 |
59 |
70 |
74 |
85 |
| 16 |
27 |
31 |
42 |
50 |
60 |
69 |
75 |
84 |
86 |
| 28 |
30 |
43 |
49 |
61 |
68 |
76 |
83 |
87 |
94 |
| 29 |
44 |
48 |
62 |
67 |
77 |
82 |
88 |
93 |
95 |
| 45 |
47 |
63 |
66 |
78 |
81 |
89 |
92 |
96 |
99 |
| 46 |
64 |
65 |
79 |
80 |
90 |
91 |
97 |
98 |
100 |
Which of the following algorithms is better. It can be implemented in any language.