Nyoj-734-odd rank cube

Source: Internet
Author: User

Cube of odd rank

Time Limit: 1000 MS | memory limit: 65535 KB

Difficulty: 3

Description
The elements of an n-level square matrix are 1, 2,..., n ^ 2. each row of the matrix is equal to the sum of the elements on the two diagonal lines. Such a square matrix is called the cube. When N is an odd number, we have one constructor called "Top right". For example, the cube at N = and 7 is given below.

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 what the "top right" means.
Input
Contains multiple groups of data. Input t to indicate that there are T groups of data. N (3 <= n <= 19) is given for one row of data in each group.
Output
For each group of data, the Order N cube is output. Each number occupies 4 cells and is right aligned.
Sample Input
2
3
5
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
Resolution:
N = 7 is used as an example. If n = 7 is used in the top right corner of the question, you can first observe the diagonal line in the oblique direction and find that the entered number is close to each other, assume that the number is filled in a [I --] [J ++] = K (K starts from 1, then observe the positions 1 and 2. we can infer that if I = 0, I = n-1 is required. After Entering 3 in the same way, we find that there is no place to fill in, then let's look at the position 4, and we will know that J should be assigned 0, that is, j = 0. You may wonder that after 7 is entered, this method is no longer applicable, so after K % N = 0, you should
I + = 2, J-= 1.
DetailsCodeAs follows:

 

# Include <stdio. h> int main () {int n, m, I, j, t, a [20] [20]; scanf ("% d", & N ); while (n --) {scanf ("% d", & M); for (I = 0, j = m/2, T = 1; t <= m * m; t ++) {A [I] [J] = T; I --; j ++; If (T % m = 0) {I + = 2; j-= 1;} if (I =-1) I = s-1; If (j = m) J = 0;} for (I = 0; I <m; I ++) {for (j = 0; j <m; 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.