蛇形填数(2)


蛇形填数(二)

时间限制: 2000 ms  |  内存限制: 65535 KB
难度: 3
描述
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<iostream>
#include<string.h>
#include<algorithm>
#define maxn 1000+10
using namespace std;
int a[maxn][maxn];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(a,0,sizeof(a));
        int n,tot=1,x=0,y=0,m;
        scanf("%d",&n);
        m=n;
        int s=n*(n+1)/2;
        a[0][0]=1;
        while(tot<s)
        {
            while(y<n-1&&!a[x][y+1]) a[x][++y]=++tot;
            while(y>0&&!a[x+1][y-1]) a[++x][--y]=++tot;
            while(x>=0&&!a[x-1][y]) a[--x][y]=++tot;
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                printf("%-2d ",a[i][j]);
            }
            printf("\n");
            m--;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值