Diagonal row of snake-filled data

Source: Internet
Author: User

/*
Question: Quantity
Solution: Simulate the filling process
Difficulties: the simulation of the filling process can be clearly considered
Key point: how to fill in the number, how to control the number of Filling
Problem solving person: lingnichong
Solution time:
Problem-solving experience: it is another kind of deformation of the snake-filled number.
*/


Time Limit: 2000/1000 ms (Java/other) memory limit: 65536/32768 K (Java/other) total submission (s): 4 accepted submission (s): 3 Font: times New Roman | verdana | georgiafont size: Regular → Problem description the elements of an n-level matrix are 1, 2 ,..., N ^ 2, each row of the column is equal to the sum of the elements on the two diagonal lines.
The square matrix is called cube. When N is an odd number, we have one constructor called "Top right". For example, the following section describes n = 3, 5, and 7.
Cube.
3
8 1 6
3 5 7
4 9 2
5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
The number in the middle of the second row is always 1, the number in the middle of the last row is n ^ 2, and the number on the right is 2. From these three cubes, you can see that "right"
What is the meaning of the above.
Input contains multiple groups of data. Input t indicates that there are T groups of data. N (3 <= n <= 19) is given for one row of data in each group.
For each group of data, output level n cube. Each number occupies 4 cells and is right aligned.
Sample Input
235
Sample output
   8   1   6   3   5   7   4   9   2  17  24   1   8  15  23   5   7  14  16   4   6  13  20  22  10  12  19  21   3  11  18  25   2   9


# Include <stdio. h> # include <string. h> int A [30] [30]; int main () {int n, m, I, J, K; scanf ("% d", & K ); while (k --) {scanf ("% d", & N); memset (A, 0, sizeof (a); I = 1; j = n/2 + 1; A [I] [J] = 1; for (m = 2; m <= N * n; m ++) {if (I = 1 & J + 1 <= N) // The first row is not the last element. {I = N; j ++; A [I] [J] = m;} else if (I = 1 & J = N) // The last element of the first line. {I ++; A [I] [J] = m;} else if (j = N & I-1> = 1) // the last column. {I --; j = 1; A [I] [J] = m ;} else if (J + 1 <= N & I-1> = 1 & A [I-1] [J + 1] = 0) {I --; j ++; A [I] [J] = m;} else if (I-1> = 1 & J + 1 <= N & A [I-1] [J + 1]! = 0) {I ++; A [I] [J] = m ;}} for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) printf ("% 4D", a [I] [J]); printf ("\ n") ;}} return 0 ;}




Contact Us

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.