Magic matrix

Magic bai matrix

The numbers 1-9 are filled in a table with three rows and three du columns, and the sum of zhuan on each row, column zhi, and two dao diagonals are all equal.

Insert picture description here

History

   魔方又称幻方、纵横图、九宫图,最早记录于我国古代的洛书。据说夏禹治水时,河南洛阳附近的大河里浮出了一只乌龟,背上有一个很奇怪的图形,古人认为是一种祥瑞,预示着洪水将被夏禹王彻底制服。后人称之为"洛书"或"河图",又叫河洛图。

   南宋数学家杨辉,在他著的《续古摘奇算法》里介绍了这种方法:只要将九个自然数按照从小到大的递增次序斜排,然后把上、下两数对调,左、右两数也对调;最后再把中部四数各向外面挺出,幻方就出现了。 (摘自《趣味数学辞典》)

   在西方,阿尔布雷特·丢勒于1514年创作的木雕《忧郁》是最早关于魔方矩阵的记载。有学者认为,魔方矩阵和风靡一时的炼金术有关。几个世纪以来,魔方矩阵吸引了无数的学者和数学爱好者。本杰明·富兰克林就做过有关魔方矩阵的实验。

So how to encode it?

#include<bits/stdc++.h>
using namespace std;
#define IN -1e6
#define INT 1e6
const int maxn=1e6;
int res=1;
typedef long long ll;
int a[1000][1000];

int main()
{
    
    
	int x,y; //坐标 
	int n; // n*n的矩阵
	cin>>n;
	x=0;
	y=n/2; 
	for(int i=1;i<=n*n;i++)
	{
    
    
		a[x][y]=i;
		if(i%n==0)
		{
    
    
			
			x=(x+1)%n;
		
		}
		else
		{
    
    
		    
			 y=(y-1+n)%n;
			 x=(x-1+n)%n;
			 
		}
	}
	
	for(int i=0;i<n;i++)
	{
    
    
		for(int j=0;j<n;j++)
		{
    
    
			cout<<a[i][j]<<' ';
		}
		putchar('\n');
	}

   return 0;
}

Guess you like

Origin blog.csdn.net/qq_52172364/article/details/112133225
Recommended