描述
1 2 3 4 5 12 13 14 6 11 15 7 10 8 9
跟蛇形填数一样,只是填数要求按照三角形填。注意每组数据之间用空行隔开
输入
第一行有一个N,表示N组测试数据
接下来每组数据包括一个数字X,表示三角形的边长,0< X <1000
输出
输出之后填好之后的图
样例输入
2 5 4
样例输出
1 2 3 4 5 12 13 14 6 11 15 7 10 8 9 1 2 3 4 9 10 5 8 6 7
#include<stdio.h>
#include<string.h>
#define maxn 1100
int a[maxn][maxn];
int main()
{
int x,y,n,tot;
memset(a,0,sizeof(a));
scanf("%d",&n);
tot=a[x=0][y=0]=1;
while(tot<n*(n+1)/2)
{
while(y+1<n && !a[x][y+1]) a[x][++y]=++tot;
while(x+1<n && y-1>=0 && !a[x+1][y-1]) a[++x][--y]=++tot;
while(x-1>=0 && !a[x-1][y]) a[--x][y]=++tot;
}
for(x=0;x<n;x++)
{
for(y=0;y<n;y++)
{
if(a[x][y])
printf("%d ",a[x][y]);
}
printf("\n");
}
}