I have a project in Golang. But i don't have any idea about how to solve it.
n will be an odd number (Feedback will be given if an odd number is not written)
- As output, a structure with n*n matrix appearance will be created according to the following rules and printed on the screen.
Rules
The rules will be transferred assuming the entered number is 3. (Any number can be entered)
- The first number will be in the middle column of the first row.
- The next number is placed in the diagonal square, to the top right.
- In such a case, the overflowing number is placed at the bottom of the column in the section it is in.
- The next number is placed in the diagonal square, to the top right.
- If the number overflows from the right, it is placed on the leftmost row.
- The next number is placed in the diagonal square, to the top right.
- If the cell is full, it is placed under the previously placed number. The number 4 is placed under the number 3.
- The next number, the number 5, is placed in the diagonal square to the top right.
- The next number, the number 6, is placed in the diagonal square to the top right.
- The next number, 7, is placed in the diagonal square to the top right.
- In case of cross overflow, it is placed below the previous number.
- For other numbers, the above rules are applied.
- Since the number 8 overflows from the right, it is placed on the far left in the line it is in.
- The number 9 overflows.
- In case of overflow, it is placed at the bottom of the column.
- All numbers are placed by operating the rules.
here's the output for n = 3
Enter the number : 3
8 1 6
3 5 7
4 9 2
I can make the zigzag matrix but I can't build an algorithm for this question. Can anyone help me about building the algorithm?
Progress:
I noticed that the sums of each row and column are equal. For example:
n=3
==> the sum of each row and column of the resulting matrix is equal to each other and to 15.n=5
==> the sum of each row and column of the resulting matrix is equal to each other and 65.n=7
==> the sum of each row and column of the resulting matrix is equal to each other and 175.n=9
==> the sum of each row and column of the resulting matrix is equal to each other and 369.