# Direct output of the snake Matrix

Source: Internet
Author: User

The question is very simple. The output is similar to the following. The requirement is that data structures such as arrays and vectors are not needed. I have seen this question before. I have always thought that the simplest method is to use formulas, you can directly find the number of positions (n, m. (Why Do We Need arrays? arrays are nothing more than saving the computing results temporarily .) Let me briefly describe my ideas. In fact, you can draw a square with a side length of N in the First quadrant of the coordinate system (N is the number of rows in your matrix ). Then draw two straight lines, one is y = x, and the other is x + y = N. The two lines divide the square into four areas (four isosceles right triangles ). The direction of the values in these four regions is fixed. If we call these four regions as top, bottom, left, and right, then: the numbers in the top triangle increase from left to right. The numbers in the right triangle increase from top to bottom. The numbers in the bottom triangle increase from right to left; the numbers in the Left triangle increase progressively from bottom to top. The regions of the four triangles can be expressed by an inequality. For example, the upper triangle can be expressed as x <= yx + y> = N. Therefore, this is easy to understand. (In this case, it may be a little inintuitive. If you want to draw a picture on paper, you can see it at a Glance. It is very simple .) You can calculate the value of each corner (top right, bottom right, bottom left, and top left), for example, the upper left corner (which is actually the part of x + y = N on the left of y = x ): f (n, m) = 4 * m * (N-m) Here m = n. The other corner calculation formulas are: f (n, m) = 2 * n * (2 * m + 1) + Nf (n, m) = (2 * n + 1) * (2 * N-2 * n-1) f (n, m) = 2*(2 * n + 1) * (m + 1)-N with the corner values, you can calculate the value of any position in any region, and there are many ways to calculate the value, for example, to calculate the number in the top triangle area, you can use the upper left corner as the reference (increase the corresponding offset, that is, m-n + 1) or the upper right corner as the reference, the two methods are equivalent. Then the program can write (python): copy the code def f (n, m): if m> = n and m + n <N: # top v = 4 * n * (N-n) + m-n + 1 elif m <= n and m + n <N: # left v = 4 * (m + 1) * (N-m-1) + m-n + 1 elif m <= n and m + n> = N: # down v = (2 * N-2 * n-1) * (2 * n + 1) + n-m elif m> = n and m + n> = N: # right v = (N-1-m) * (4 * m + 1) + N + n else: raise Exception ("Unknown area :{},{}". format (n, m) return vfor n in range (0, N): for m in range (0, N): print ("{0: 3d }". format (f (n, m), end = '') print ()

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.