简单魔方程序

一个魔方,就是一个由1到n^2的整数构成的n*n的矩阵,其中每行、每列以及两个主对角线上的数字之和都相等。

初见这个定义,最难的地方便是如何控制每行每列的数字和相等。当n为奇数时,把1放入第一行最中间的方格中。向左上方移动,并按照数字的递增顺序,把数字填入空方格。如果移出了方格(即超过了魔方的边界),则进入魔方对边的对应方格。如果一个方格已经被填入数字,则向下继续填写方格。图中给出了当n=7时魔方。

2819101483930
292718974738
373526178646
4536342516145
4444233241513
1234341322314
2011242403122

根据H.Coxeter法则,还有以下问题待解决:    

1.如何控制输入数字向左上方移动;

2.如何控制在溢出方格时进入魔方对边的对应方格;

3.如何控制当已经有数字时向下填写方格。


其实这些问题并不难解决。只需要控制向左上循环输入时进行条件判定即可。

代码如下,在vs2013上编译运行通过。

#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 15

int main(void)
{
	static int square[MAX_SIZE][MAX_SIZE];
	int i, j, row, column;
	int count;
	int size;

	printf("Enter the size of the square: ");
	scanf("%d", &size);

	if (size<1 || size>MAX_SIZE + 1)
	{
		fprintf(stderr, "Error! Size is out of range\n");
		exit(1);
	}
	if (!(size % 2))
	{
		fprintf(stderr, "Error! Size is even\n");
		exit(1);
	}
	for (i = 0; i < size; i++)
	{
		for (j = 0; j < size; j++)
		{
			square[i][j] = 0;
		}
	}
	square[0][size / 2] = 1;

	i = 0;
	j = (size / 2);
	for (count = 2; count <= size*size; count++)
	{
		row = (i - 1 < 0) ? (size - 1) : (i - 1);
		column = (j - 1 < 0) ? (size - 1) : (j - 1);
		if (square[row][column])
			i = (++i) % size;
		else
		{
			i = row;
			j = (j - 1 < 0) ? (size - 1) : --j;
		}
		square[i][j] = count;
	}
	
	printf("Magic Square of size %d:\n\n", size);
	for (i = 0; i < size; i++)
	{
		for (j = 0; j < size; j++)
			printf("%5d", square[i][j]);
		printf("\n");
	}
	printf("\n\n");
	system("pause");
	return 0;
}
也可以使用用户自定义函数编写,这样可以增加可读性,但是其时间复杂性会增加。

使用自定义函数改写如下

#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 15

int input();
void initialization(int, int(*)[MAX_SIZE]);
void function(int, int(*)[MAX_SIZE]);
void output(int, int(*)[MAX_SIZE]);

int main(void)
{
	static int square[MAX_SIZE][MAX_SIZE];
	int size;
	size = input();
	initialization(size, square);
	function(size, square);
	output(size, square);
	system("pause");
	return 0;
}

int input()
{
	int i, j, row, column;
	int count;
	int size;

	printf("Enter the size of the square: ");
	scanf("%d", &size);

	if (size<1 || size>MAX_SIZE + 1)
	{
		fprintf(stderr, "Error! Size is out of range\n");
		exit(1);
	}
	if (!(size % 2))
	{
		fprintf(stderr, "Error! Size is even\n");
		exit(1);
	}

	return size;
}

void initialization(int size,int (*square)[MAX_SIZE])
{
	int i, j;
	for (i = 0; i < size; i++)
	{
		for (j = 0; j < size; j++)
		{
			square[i][j] = 0;
		}
	}
}

void function(int size, int(*square)[MAX_SIZE])
{
	int i, j;
	int column, row;
	int count;
	square[0][size / 2] = 1;

	i = 0;
	j = (size / 2);
	for (count = 2; count <= size*size; count++)
	{
		row = (i - 1 < 0) ? (size - 1) : (i - 1);
		column = (j - 1 < 0) ? (size - 1) : (j - 1);
		if (square[row][column])
			i = (++i) % size;
		else
		{
			i = row;
			j = (j - 1 < 0) ? (size - 1) : --j;
		}
		square[i][j] = count;
	}
}

void output(int size, int(*square)[MAX_SIZE])
{
	int i,j;
	printf("Magic Square of size %d:\n\n", size);
	for (i = 0; i < size; i++)
	{
		for (j = 0; j < size; j++)
			printf("%5d", square[i][j]);
		printf("\n");
	}
	printf("\n\n");
}
这样整个程序的可读性会提升不少,读起来会更轻松。

附运行截图一张


参考:  Fundamentals of Data Structures in C__Ellis Horowitz



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值