problem
Description
Input
Output
Sample Input
3
Sample Output
8 1 6
3 5 7
4 9 2
Data Constraint
analysis
真心希望明天的NOIPTG第一题像这题和NOIP2014的石头剪刀布这么水——
亏人家洛谷把这道签到题标上普及-的难度
题目都给了你方法求幻方,直接模拟就可以了,5分钟不到的码量……我也是醉了
时间复杂度O(n)
code
#include<cstdio>
using namespace std;
int a[40][40];
int n;
int main()
{
freopen("magic.in","r",stdin);
freopen("magic.out","w",stdout);
scanf("%d",&n);
a[1][n/2+1]=1;
int nowx=1,nowy=n/2+1;
for (int i=2;i<=n*n;i++)
{
if (nowx==1 && nowy!=n)nowx=n,nowy+=1;
else if (nowy==n && nowx!=1)nowy=1,nowx-=1;
else if (nowx==1 && nowy==n)nowx+=1;
else if (nowx!=1 && nowy!=n)
{
if (a[nowx-1][nowy+1]==0)nowx-=1,nowy+=1;
else nowx+=1;
}
a[nowx][nowy]=i;
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}