C語言魔方陣的三種實現方法

魔方陣:

把1到n*n排成n行n列方陣,使方陣中的每一行、每一列以及對角線上的數之和都相同,即為n階魔方陣。

根據魔方陣的規律,我將它分為三種情況。

1.奇數階魔方陣 

規律:第一個數放在第一行的中間,下一個數放在上一個數的上一行下一列,若該位置已經有瞭數字即放在上個數的下面一行的相同列

用C語言編程如下:

示例:n=5;

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
 void Magic1()
{
#define ROW 5
#define COL ROW
assert(ROW % 2 != 0);  //判斷n是否為奇數
 
int arr[ROW][COL] = { 0 }; //定義二維數組
 
int currow = 0;
int curcol = COL / 2;
arr[currow][curcol] = 1;
for (int i = 2; i <= ROW * COL; i++) 
{
	if (arr[(currow - 1 + ROW) % ROW][(curcol + 1) % COL] == 0) //按照規律賦值
	{
		currow = (currow - 1 + ROW) % ROW;
		curcol = (curcol + 1) % COL;
	}
	else 
	{
		currow = (currow + 1) % ROW;
	}
	arr[currow][curcol] = i;
}
 
for (int i = 0; i < ROW; i++)  //打印魔方陣
{
	for (int j = 0; j < COL; j++)
	{
		printf("%-3d", arr[i][j]);
	}
	printf("\n");
}
 
}
int main()
{
	 
	Magic1();
	return 0;
}

結果:

2.偶數階魔方陣 (n=4K)

規律:按數字從小到大,即1,2,3……n順序對魔方陣從左到右,從上到下進行填充;
將魔方陣分成若幹個4×4子方陣(如:8階魔方陣可分成四個4×4子方陣),將子方陣對角線上的元素取出;將取出的元素按從大到小的順序依次填充到n×n方陣的空缺處。

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//偶數魔方陣 4K 
void Magic2()
{
#define ROW 8
#define COL ROW
	int tmp = 1;
	int arr[ROW][COL] = { 0 };  //定義二維矩陣
	for (int i = 0; i < ROW; i++) 
	{
		for (int j = 0; j < COL; j++)
		{
			arr[i][j] = tmp++;
		}
	}
	int row1 = 1;
	int col1 = 1;
 
	int row2 = 1;
	int col2 = 1;
 
	for (int i = 0; i < (ROW / 4) ; i++)
	{
		for (int j = 0; j < (COL / 4); j++)
		{
			row1 = 4 * i;
			col1 = 4 * j;
			row2 = 4 * i;
			col2 = 4 * j + 3;
			for (int k = 0; k < 4; k++)
			{
				arr[row1][col1] = (ROW * COL + 1) - arr[row1][col1];
				arr[row2][col2] = (ROW * COL + 1) - arr[row2][col2];
				row1++;
				col1++;
				row2++;
				col2--;
			}
		}
	}
 
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			printf("%-3d", arr[i][j]);
		}
		printf("\n");
	}
 
}
int main()
{
	Magic2();
	return 0;
}

結果: 

3.偶數階魔方陣 (n=4K+2)

規律:

3.1.填充規則

將魔方分成A、B、C、D四個k階奇方陣, 利用奇數魔方陣填充方法依次將A、D、B、C填充 。

3.2.交換規則      上下標記的數字進行交換
1.右半邊大於k+2的列(從1開始)
2.左半邊,上下兩個塊最中心的點進行交換
3.左半邊小於中心列的列(除瞭上下半邊最中心的行的第一列的那個值不用交換)(從1開始)

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
 
void Magic3()
{
#define ROW 10 
#define COL ROW
	assert(ROW % 2 == 0 && ROW % 4 != 0);
	int arr[ROW][COL] = { 0 };
	//左上角
	int currow = 0;
	int curcol = ROW/4;
	arr[currow][curcol] = 1;
	int tmp = 0;
	for (int i = 2; i <= ROW * COL/ 4; i++)
	{
		if (arr[(currow - 1 + ROW / 2) % (ROW / 2)][(curcol + 1) % (COL / 2)] == 0)  //判斷上一行下一列是否被賦值
		{
			currow = (currow - 1 + ROW / 2) % (ROW / 2);
			curcol = (curcol + 1) % (COL / 2);
		}
		else
		{
			currow = (currow + 1) % (ROW / 2);
 	
		}
		arr[currow][curcol] = i;
	}
 
	//右下角
	currow = ROW / 2;
	for (int i = 0; i < ROW / 2; i++, currow++)
	{
		curcol = COL / 2;
		for (int j = 0; j < COL / 2; j++, curcol++)
		{	
			arr[currow][curcol] = arr[i][j] + 9;
		}
	}
	//右上角
	currow = 0;
	for (int i = ROW/2; i < ROW ; i++, currow++)
	{
		curcol = COL / 2;
		for (int j = COL/2; j < COL; j++, curcol++)
		{	
			arr[currow][curcol] = arr[i][j] + 9;
		}
	}
	//左下角
	currow = ROW / 2;
	for (int i = 0; i < ROW/2; i++, currow++)
	{
		curcol = 0;
		for (int j = COL/2; j < COL; j++, curcol++)
		{
			arr[currow][curcol] = arr[i][j] + 9;
		}
	}
 
	//替換規則1:右半邊 大於k+2的列  進行上下交換
	for (int i = 0; i < ROW / 2; i++)
	{
		for (int j = ROW / 2 + ROW / 4 + 2; j < COL; j++)
		{
			tmp = arr[i][j];
			arr[i][j] = arr[i + ROW / 2][j];
			arr[i + ROW / 2][j] = tmp;
		}
	}
	//替換規則2:交換左半邊,兩個中心節點
	currow = ROW / 4;
	curcol = COL / 4;
	tmp = arr[currow][curcol];
	arr[currow][curcol] = arr[currow + ROW / 2][curcol];
	arr[currow + ROW / 2][curcol] = tmp;
 
	//替換規則3:左半邊,除(K+1,1)這個點外,小於k+1的列  上下交換
	for (int j = 0; j < ROW / 4; j++) //表示交換的列
	{
		for (int i = 0; i < ROW / 2; i++) //表示交換的行
		{
			if (i == ROW / 4 && j == 0)
			{
				continue;
			}
			else
			{
				tmp = arr[i][j];
				arr[i][j] = arr[i + ROW / 2][j];
				arr[i + ROW / 2][j] = tmp;
			}
		}
	}
	//打印
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			printf("%-3d", arr[i][j]);
		}
		printf("\n");
	}
}
 
int main()
{
	Magic3();
	return 0;
}

結果: 

到此這篇關於C語言魔方陣的三種實現方法的文章就介紹到這瞭,更多相關C語言魔方陣內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: