H - A Simple Triangle(水题)

H - A Simple Triangle

NBUT - 1226

In this problem you need to make a triangle ,just like the sample out. The element in the ith row and jth column

should be the product(乘积) of i and j.

Input

The first line of input is an integer C which indicate the number of test cases.
Then C test cases follow.Each test case contains an integer N (1<=N<=20) in a line which mentioned above.

Output

For each test case, print out the triangle. the triangle separated by a blank line.
If the product is more than 9 (product > 9) you should print a space, or you should print two space.The final number of each line don’t need print space.

Sample Input

3
5
6
7

Sample Output

1  2  3  4  5
2  4  6  8
3  6  9
4  8
5

1  2  3  4  5  6
2  4  6  8  10
3  6  9  12
4  8  12
5  10
6

1  2  3  4  5  6  7
2  4  6  8  10 12
3  6  9  12 15
4  8  12 16
5  10 15
6  12
7

题意:

t t t组输入,每组输入输入一个 n n n,那么你将输出一个形似三角形的东西,第一行有 n n n个数,第二行有 n − 1 n-1 n1个数,以此类推,直到第 n n n行。其中第 r r r行第 c c c列的值为 c ∗ r c*r cr

不过需要注意的是: 如果这个数<=9,后面则输出两个空格,否则输出一个空格,每行的最后一个数后面没有空格。且每两个三角行之间隔一个空行。

PE到shi

代码:

#include<queue>
#include<iostream>
#include<string.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define mset(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
const int maxn=2e4+100;
int t;
/*
如果最后一个数,后面没有空格
如果这个数<9 后面两个空格
否则后面一个空格
*/
int main()
{
    int cas=0,n=0;
    scanf("%d",&t);
    while(t--)
    {
        if(cas)
            puts("");
        cas=1;
        scanf("%d",&n);
        for(int r=1; r<=n; ++r)
        {
            for(int c=1; c<=n-r+1; ++c)
            {
                if(c==n-r+1)
                    printf("%d",r*c);
                else if(r*c<=9)
                    printf("%d  ",r*c);
                else
                    printf("%d ",r*c);
            }
            puts("");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值