-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangle.cpp
40 lines (37 loc) · 844 Bytes
/
triangle.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
#include <iostream>
#include <fstream>
using namespace std;
const int NUM_ROWS = 100;
const 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);
// "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';
return 0;
}