-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.cpp
More file actions
63 lines (60 loc) · 1.3 KB
/
trace.cpp
File metadata and controls
63 lines (60 loc) · 1.3 KB
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
#include <iostream>
#include <fstream>
using namespace std;
const unsigned int NUM_ROWS = 100;
const unsigned int TOTAL_ELEM = (1 + NUM_ROWS) * NUM_ROWS * 0.5;
void readFile(unsigned int tri[])
{
ifstream inFile;
inFile.open("triangle.txt");
for (int i = 0; i < TOTAL_ELEM; ++i)
{
inFile >> tri[i];
}
inFile.close();
}
int main()
{
unsigned int tri[TOTAL_ELEM];
readFile(tri);
unsigned int in[TOTAL_ELEM];
for (int i = 0; i < TOTAL_ELEM; i++)
{
in[i] = tri[i];
}
// "base case" for the DP problem is row 100
// starting filling out the solution "bottom up" starting from last
// element on row 99
int row = NUM_ROWS - 1;
int count = 0;
for (int i = TOTAL_ELEM - NUM_ROWS - 1; i >= 0; i--)
{
tri[i] += ((tri[i+row] > tri[i+row+1]) ? tri[i+row] : tri[i+row+1]);
count++;
if (count == row)
{
count = 0;
row--;
}
}
cout << "The largest sum is: " << tri[0] << '\n';
unsigned int sum = tri[0];
unsigned int sol[NUM_ROWS];
int index = 0;
for (int i = 0; i < TOTAL_ELEM; i++)
{
if (tri[i] == sum)
{
sol[index++] = in[i];
cout << in[i] << '\n';
sum -= in[i];
}
}
unsigned int cumSum = 0;
for (int i = 0; i < NUM_ROWS; i++)
{
cumSum += sol[i];
}
cout << "Double check: " << cumSum << '\n';
return 0;
}