-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.cc
42 lines (41 loc) · 1.01 KB
/
13.cc
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
/*Find first 10 digits of sum of 100 50-digit numbers
The 10 first digits will originate from at most the
12 fist digits of the 100 50-digit numbers. Hence
one can discard all numbers after the first 12.
Those numbers summated will at most become a 14-digit
number, which can be reprensented by 47-bits. This fits
in a double.
*/
#include <string>
#include <list>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]){
if(argc == 2){
fstream fin;
fin.open(argv[1], ifstream::in);
string elem;
vector<double> vec;
string::size_type sz;
//Read numbers line by line into array.
//Only keep first 12 numbers
while(!fin.eof()){
getline(fin,elem);
elem = elem.substr(0,12);
cout << elem << "\n";
vec.push_back(stod(elem, &sz));
}
//sum all numbers
double sum = 0;
for (int i = 0; i < 100; i++){
sum += vec[i];
}
elem = to_string(sum);
cout << elem << "\n";
elem = elem.substr(0,10);
cout << elem << "\n";
}
}