H - A Simple Triangle
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 n−1个数,以此类推,直到第 n n n行。其中第 r r r行第 c c c列的值为 c ∗ r c*r c∗r。
不过需要注意的是: 如果这个数<=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;
}