Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the implementation methods of magic square matrix in C language?

Shulou Source: shulou.com Published: 2022-06-02 07:39:51 09月27日 Update

This article mainly introduces the C language magic square matrix implementation methods, the article is very detailed, has a certain reference value, interested friends must read it!

Magic square:

Arrange 1 to n squares into n rows and n columns, so that the sum of the numbers on each row, column and diagonal in the square matrix is the same, that is, the magic square of order n.

According to the law of the magic square, I divide it into three cases.

1. Odd order magic matrix

Rule: the first number is placed in the middle of the first row, and the next number is placed in the next column of the previous number. If there is already a number in that position, it is the same column in the lower row of the previous number.

Programming in C language is as follows:

Example: nasty 5

# include#include#include void Magic1 () {# define ROW 5#define COL ROWassert (ROW% 2! = 0); / / determine whether n is odd [ROW] [COL] = {0}; / / define a two-dimensional array int currow = 0 currow curcol = COL / 2 currow [curcol] = 1 for (int I = 2; I)

Tags: Matrix half jacks one line up and down rules rules number method language even odd number result same two elements content diagonal diagonal Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Huawei NVidia Shulou Information Shulou Technology