被玩坏的数字三角(模拟)

181 篇文章 0 订阅
171 篇文章 0 订阅

Link:http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1007&cid=25995



被玩坏的数字三角

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 11   Accepted Submission(s) : 8
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

给出一个数字n,要求输出这样的三角形,如n=6时,输出

这个三角形有如下的特征: 
1.行数为 n,即6行。
2.(1,1)位置上的数字为1
3.三角形上的数字按照图中所示方向递增。

Input

第一行输入一个正整数T,代表数据的组数。每组数据包括一行,包括一个正整数n(t,n<=100),代表三角形的行数。

Output

输出题目要求的三角形。

Sample Input

1
6

Sample Output

1
2 15
3 16 14
4 17 21 13
5 18 19 20 12
6 7 8 9 10 11

Author

wzx

#include<iostream>
#include<istream>
#include<ostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#include<map>
using namespace std;
int m[111][111];
int main()
{ 
    int num,ti,tj,j,i,n,s,h,t,cntj,cnti;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&h);
        n=h;
        num=0;
        ti=1;
        tj=2;
        cntj=0;
        cnti=1;
        s=(1+n)*n/2;
        while(1)
        {
            for(i=ti;i<=n;i++)
            {
                m[i][cnti]=++num;
            }
            ti+=2;
            cnti++;
            for(j=tj;j<=n-cntj;j++)
            {
                m[n][j]=++num;
            }
            tj++;
            cntj++;
            for(i=n-1;i>=ti-1;i--)
            {
                m[i][i-cntj+1]=++num;
            }
            if(num==s)
                break;
            n--;
        }
        for(i=1;i<=h;i++)
        {
            for(j=1;j<=i-1;j++)
            {
                printf("%d ",m[i][j]);
            }
            printf("%d\n",m[i][i]);
        }
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值