游客 Signup | Login
中文 | En

2733 - 蛇形填数

n*n方陈里填入1,2,...,n*n,要求填成蛇形。例如n=4时方陈为:

10 11 12 1 
9 16 13 2 
8 15 14 3 

<span>7 6 5 4&nbsp;</span>

Input

直接输入方陈的维数,即n的值。(n<=100)

Output

输出结果是蛇形方陈。(每个数字后均有一空格,方便输出)

Examples

Input

3

Output

7 8 1 
6 9 2 
5 4 3 

Solution C

#include<stdio.h>
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
	int a,b,c,d,n,sum=1;
	int yi[101][101];
	scanf("%d",&n);
	for(a=0;a<=(n-1)/2;a++)
	{
		for(b=a;b<=n-a-1;b++)
			yi[b][n-a-1]=sum++;
		for(b=n-2-a;b>=a;b--)
			yi[n-a-1][b]=sum++;
		for(b=n-a-2;b>=a;b--)
			yi[b][a]=sum++;
		for(b=a+1;b<n-a-1;b++)
			yi[a][b]=sum++;
	}
	for(c=0;c<n;c++)
	{
		for(d=0;d<n;d++)
			printf("%d ",yi[c][d]);
		printf("\n");
	}
}

Time Limit 1 second
Memory Limit 32 MB
Discuss Stats
上一题 下一题