现在的位置: 首页 > 综合 > 正文

hdu2183

2013年10月20日 ⁄ 综合 ⁄ 共 744字 ⁄ 字号 评论关闭

/*
分析:
    无聊找规律水题。。。
    自己找了个规律,弄出来一个n*n方阵,满足“排成n*n,方阵
每行每列每条对角线上的n个数字之和s相等”,但WA了,因为这题
不是special judge,必须按照这个题的那种规律才行。。。
    把1排在中心值下放,然后向右下递进,当next已经填过后,
让next_x=now_x+2,然后继续。。。

                                                    2012-11-12
*/

#include"stdio.h"
#include"string.h"
int num[25][25];
int main()
{
	int n;
	int i,l;
	int count;
	int now,now_x,now_y;
	int next,next_x,next_y;
	while(scanf("%d",&n),n)
	{
		count=n*n;
		memset(num,0,sizeof(num));
		next=1;
		next_x=n/2+2;
		next_y=n/2+1;
		while(count--)
		{
			now=next;
			now_x=next_x;
			now_y=next_y;
			num[now_x][now_y]=now;

			next=now+1;
			next_x=now_x+1;
			while(next_x>n)	next_x-=n;
			next_y=now_y+1;
			while(next_y>n)	next_y-=n;
			if(num[next_x][next_y])
			{
				next_x=now_x+2;
				while(next_x>n)	next_x-=n;
				next_y=now_y;
			}
		}

		for(i=1;i<=n;i++)
		{
			for(l=1;l<=n;l++)	printf("%4d",num[i][l]);
			printf("\n");
		}
	}
	return 0;
}

抱歉!评论已关闭.