比赛 20190908之惊鸿 评测结果 AAAAAAAAAA
题目名称 神奇的幻方 最终得分 100
用户昵称 瑆の時間~無盡輪迴·林蔭 运行时间 0.007 s
代码语言 C++ 内存使用 13.66 MiB
提交时间 2019-09-08 13:09:52
显示代码纯文本
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	freopen("2015magic.in","r",stdin);
	freopen("2015magic.out","w",stdout);
	int n,m;
	cin>>n;
	m=n*n;
	int map[40][40];
	int h,c;
	h=1;
	c=n/2+1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			map[i][j]=0;
		}
	}
	map[h][c]=1;
	for(int i=2;i<=m;i++)
	{
		if(h==1&&c!=n)
		{
			h=n;
			c++;
			map[h][c]=i;
			//cout<<i<<' '<<h<<' '<<c<<endl;
			continue;
		}
		if(h!=1&&c==n)
		{
			c=1;
			h--;
			map[h][c]=i;		
		//	cout<<i<<' '<<h<<' '<<c<<endl;
			continue;
		}
		if(h==1&&c==n)
		{
			h++;
			map[h][c]=i;
			//cout<<i<<' '<<h<<' '<<c<<endl;
			continue;
		}
		if(h!=1&&c!=n)
		{
			if(map[h-1][c+1]==0)
			{
				h--;
				c++;
				map[h][c]=i;
				//cout<<i<<' '<<h<<' '<<c<<endl;
				continue;
			}
			else
			{
				h++;
				map[h][c]=i;
				//cout<<i<<' '<<h<<' '<<c<<endl;
				continue;
			}
		} 
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cout<<map[i][j]<<' ';
			
		}
		cout<<endl;
	}
	return 0;
}