-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCTDL017.cpp
80 lines (73 loc) · 1.53 KB
/
SCTDL017.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// create a triangle from an array
// this triangle has n rows
// i-th row is sum of two consecutive elements in (i-1)-th
/* Example:
[1 2 3 4 5]
[3 5 7 9]
[8 12 16]
[20 28]
[48]
*/
/*
Author: Long Hoang Thanh
Date: 8/11/2022
*/
#include<iostream>
#include<vector>
using namespace std;
void solve(vector<int> vt_starting, int n);
void printRes(vector<vector<int> > triangle);
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
vector<int> vt_starting;
for(int i=0; i < n; i++)
{
int tmp;
cin >> tmp;
vt_starting.push_back(tmp);
}
solve(vt_starting, n-1);
cout << endl;
}
}
void solve(vector<int> vt_starting, int n)
{
vector<vector<int> > triangle;
vector<int> tmp;
triangle.push_back(vt_starting);
for(int i=0; i < n; i++)
{
for(int j = 1; j < triangle[i].size(); j++)
{
int sum_tmp;
sum_tmp = triangle[i][j] + triangle[i][j-1];
tmp.push_back(sum_tmp);
}
triangle.push_back(tmp);
tmp.clear();
}
printRes(triangle);
}
void printRes(vector<vector<int> > triangle)
{
//print result
for(int i=triangle.size() - 1; i >= 0; i--)
{
cout << '[';
for(int j = 0; j < triangle[i].size(); j++)
{
cout << triangle[i][j];
if(j != triangle[i].size() - 1)
{
cout << " ";
}
}
cout << ']' << " ";
}
}