- 博客(0)
- 资源 (1)
- 收藏
- 关注
三家蛇形填数
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1100;
int a[maxn][maxn];
int main(){
int n,x;
scanf("%d",&n);
while(n--){
scanf("%d",&x);
memset(a,0,sizeof(a));
int num=1;
int c=0,r=0;
a[r][c]=num;
while(num<(x*(x+1)/2)){
while(c+1<x&&!a[r][c+1]) a[r][++c]=++num;
while(c-1>=0&&r+1<x&&!a[r+1][c-1]) a[++r][--c]=++num;
while(r-1>=0&&!a[r-1][c]) a[--r][c]=++num;
}
for(r=0;r<x;r++)
{
for(c=0;c<x;c++)
{
if(a[r][c]!=0)
{
if(c!=0) printf(" ");
printf("%d",a[r][c]);
}
}
printf("\n");
}
printf("\n");
}
return 0;
}
2019-02-27
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人