Original questions see the http://www.cnblogs.com/nokiaguy/archive/2009/07/24/1530139.html of the galaxy messenger
First impression I got the same solution as him, that is, using oblique slices, the X-1 of each layer and Y + 1 to Control which coordinates the pen that writes numbers in sequence falls.
However, based on some observations over a period of time, I have discovered several patterns that will not allow us to simulate the whole Sequential stream.
First, observe each slice.
| 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 |
We found that each number on the N and N coordinates is symmetric and average. After simple calculation, we found that the average values of 1-N slices are (N * n + 1)/2.
Continue to observe and find the rule again. Any two grids in the diagonal symmetry of this matrix are exactly two points of equal distance from the center of the entire series, for example, 1, 100, 20, and 81. Their sum must be Max * MAX + 1.
In this way, we can use the above two features to generate the entire matrix as long as we can calculate the values of a row or column.
However, the rows and columns of this matrix are too messy. It is difficult to find out the law. At this time, we need to consider a simpler model.
Let's look at the matrix of non-first-end relays.
1
1 2
3
1 2 4
3 5
6
1 2 4 7
3 5 8
6 9
10
Did you find that the numbers in bold are the sum of all the previous oblique slices?
So what are the similarities and differences between the sequence matrix and the relay matrix?
| 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 |
We can regard the relay matrix as the sequence matrix for line-based inversion. In this way, we get a reference value for swinging on the X and Y axes. With the reference value, the matrix can be generated based on the first two points.
Code
Static Void Main ( String [] ARGs)
{
VaR Max = 11 ;
Int [,] MX = New Int [Max, Max];
VaR Sideval = 0 ;
For (VAR Seed = 0 ; Seed < Max; Seed ++ )
{
Int Parevalue = (Seed + 1 ) * (Seed + 1 ) + 1 ; // Law 1 the sum of the two axial symmetry numbers in the center is equal to the number of layers + 1
Bool Isodd = Seed % 2 = 1 ;
Sideval = Sideval + Seed + 1 ; // Rule 2 has at least one number on both sides: 1 + 2 + 3 + 4 + n
For (VAR fillseed = Seed; fillseed > = 0 ; Fillseed -- )
{
Int Row, Col;
If ( ! Isodd)
{
Row = Fillseed;
Col = Seed - Fillseed;
}
Else
{
Col = Fillseed;
Row = Seed - Fillseed;
}
// Law 1 the sum of the two axial symmetry numbers in the center is equal to the number of layers + 1
MX [col, row] = Sideval - Fillseed;
MX [row, Col] = Parevalue - MX [col, row];
// Law 3 the sum of center symmetry is Max * MAX + 1
MX [Max - Col - 1 , Max - Row - 1 ] = Max * Max + 1 - MX [col, row];
MX [Max - Row - 1 , Max - Col - 1 ] = Max * Max + 1 - MX [row, Col];
}
}
For (VAR x = 0 ; X < Max; x ++ )
{
For (Var y = 0 ; Y < Max; y ++ )
{
Console. Write ( String . Format ( " {0: D3} " , MX [x, y]);
}
Console. writeline ();
}
Console. Read ();
}
There are many such solutions that are undoubtedly quite cool. As an interview question, it is not recommended to write such a solution that the examiner can hardly understand.
It makes some sense to use brainstorming to prevent Alzheimer's disease.