NBUT-1226 A Simple Triangle

[1226] A Simple Triangle
时间限制: 1000 ms 内存限制: 65535 K
问题描述
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.

输入
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.
输出
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.
样例输入
3
5
6
7
样例输出
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

一道PE到质疑人生的题目。
这题的大致意思就是输出第i行j列的乘积(i*j),这个乘积如果大于9的话后面要加一个空格,否则加两个空格,还有就是每行的最后一个结果后面不加空格。
这题还有一个坑点就是最后的换行,我就是被坑在了这里。

AC代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n+1-i;j++)
            {
                if(j==n+1-i) cout<<i*j;
                else if(i*j<=9) cout<<i*j<<"  ";
                else cout<<i*j<<" ";
            }
            cout<<endl;
        }
        if(t) cout<<endl;
    }
    return 0;
}

PE代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n+1-i;j++)
            {
                if(j==n+1-i) cout<<i*j;
                else if(i*j<=9) cout<<i*j<<"  ";
                else cout<<i*j<<" ";
            }
            cout<<endl;
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44137005/article/details/88895027