-->
当前位置:首页 > 题库

PROGRAMMING:magic square

Luz5年前 (2021-05-10)题库513
####Task description
```
N-order magic square: the so-called n-order magic square refers to the square matrix composed of 1 to n * n continuous natural numbers, and the sum of each row, column and diagonal is equal.
For example, the 7-order magic cube is:
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
When n is odd, n magic square construction algorithm is as follows:
(1) put 1 in the middle column of the first row;
(2) from 2 to n × The numbers up to n are stored according to the following rules: press 45 ° Direction to the right up, each number of rows than the previous number of rows minus 1, the number of columns plus 1.
(3) if the range of row and column is beyond the range of matrix, wrap.
(4) if there is a number in the position determined according to the above rules, put the next number below the previous one.
```
Programming output n magic square.
####Input format:
A positive odd number n, (0 < n < 20).
####Output format:
Output n-order magic square, each integer occupies 5 columns.
####Input example:
```in
seven
```
####Output example:
```out
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
```
######Input example:
```in
three
```
######Output example:
```out
8 1 6
3 5 7
4 9 2
```






answer:If there is no answer, please comment