Output a spiral Queue according to the number of circles N, as shown in figure
To better illustrate this algorithm, I will draw another image.
First, let's talk about this figure.
We place the graph on the X and Y axes, then the right side is the X positive half axis, and the left side is the X negative half axis. The top is the positive Half Axis of Y, and the bottom is the negative Half Axis of Y.
Then the entire graph is divided into four upper and lower left areas by a big red cross.
Start analysis:
1. The Code clearly obtains data based on the (x, y) coordinates. Therefore, we can write the code of the main function.
2. when writing the spiral function, we obviously found that rightup = (2 * n + 1) * (2 * n + 1) and leftdown = (2 * n) in the lower left corner) * (2 * n) + 1
3. Then we should first calculate n = max (ABS (x), ABS (y ));
4. then obtain the values in the lower left corner and upper right corner. (In this case, the values in the same column with him can be calculated [except the values below the upper right corner ])
5. Then, the area is divided based on the comparison between N and X and Y. That's right. It's divided into four areas: upper, lower, and left.
6. Now there is a big problem, that is, we can get the values of the same row or the same column based on the data on the right and bottom left. However, we cannot [the value of the right region] From the value in the upper right corner to the value below the upper right corner, because it can be said that the value of this region has nothing to do with the value in the lower left corner and the value in the upper right corner, for simplicity, we will not discuss it anymore ]. How can this problem be solved? It is actually very simple. We only need to judge the upper and lower left first. When we finally judge the right, the conditions of equal X and Y have been filtered out by the first three. You can. So we defined the following code:
# Define max (A, B) (a)> (B )? (A) (B ))
# Define ABS (x)> 0? (X):-(x ))
7. Therefore, the depth of mathematics skill determines how far programmers can go. This is what our teacher said. I am more and more familiar with the meaning of this sentence.
The Code is as follows:
# Include <stdio. h> # define max (A, B) (a)> (B )? (A) :( B) # define ABS (x)> 0? (X):-(x) int spiral (int x, int y) {int result; // int n = max (ABS (x ), ABS (y); // return a large value, which is not only the number of layers in the current position nint rightup = (2 * n + 1) * (2 * n + 1 ); // return the value in the upper-right corner of the number of layers, which is also the maximum value of the square int leftdown = 4 * n + 1; // return the value of the lower left corner of the layer if (n =-x) {result = leftdown + ABS (x-y);} else if (n = y) {result = rightup-ABS (x-y);} else if (n =-y) {result = leftdown-ABS (x-y );} else {result = leftDown-2 * n-ABS (x + y);} return result;} int main (void) {// output spiral queue int n, x, y; printf ("Enter the number of circles in the spiral queue N:"); scanf ("% d", & N); For (y = N; y> =-N; y --) {for (x =-N; x <= N; X ++) {printf ("% 8d", spiral (x, y ));} printf ("\ n");} return 0 ;}