-
Notifications
You must be signed in to change notification settings - Fork 0
/
Euler18.cpp
54 lines (46 loc) · 928 Bytes
/
Euler18.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
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <math.h>
#include <unistd.h>
#include <fstream>
using namespace std;
void print2D(vector<vector<int> > v){
for(unsigned int i = 0; i < v.size(); i++){
for(unsigned int j = 0; j < v[i].size(); j++){
if(j != v[i].size() -1 ){
cout<<v[i][j]<<", ";
}
else{
cout<<v[i][j]<<endl;
}
}
}
}
int solve(vector<vector<int> > t, int i, int j){
if(i == t.size()-1){
return t[i][j];
}
return t[i][j] + max(solve(t, i+1, j), solve(t, i+1, j+1));
}
int main(){
vector<vector<int> > triangle;
ifstream in_str("Euler18.txt");
string tri;
int len = 1;
vector<int> temp;
while(in_str>>tri){
int num = atoi(tri.c_str());
if(temp.size() < len){
temp.push_back(num);
}
else{
triangle.push_back(temp);
temp.clear();
temp.push_back(num);
len += 1;
}
}
triangle.push_back(temp);
cout<<solve(triangle, 0, 0)<<endl;
}