-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec1.cpp
More file actions
43 lines (28 loc) · 870 Bytes
/
Dec1.cpp
File metadata and controls
43 lines (28 loc) · 870 Bytes
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>
#include <string>
#include <vector>
#include <algorithm>
int main(){
std::ifstream file("data1.txt");
std::string str{};
std::vector<int> calories{};
int cur_cal{};
//GO THROUGH THE LINES OF THE DATA
while (getline(file,str)){
//IF THE LINE IS NOT EMPTY, SUM THE VALUE TO THE CURRENT TOTAL
if (str.size()>0){
cur_cal += std::stoi(str);
}
//IF THE LINE IS EMPTY, SAVE THE CURRENT TOTAL AND SET IT TO ZERO FOR NEXT ITERATION
else{
calories.push_back(cur_cal);
cur_cal = 0;
}
}
//SORT ALL THE CALORIES IN DECREASING ORDER
std::sort(calories.begin(),calories.end(),std::greater<int>());
//SUM THE THREE LARGEST VALUES
std::cout<<calories[0]+calories[1]+calories[2]<<'\n';
return 0;
}