-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.cpp
72 lines (50 loc) · 1.05 KB
/
8.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product.
What is the value of this product?
*/
#include <iostream>
#include <string>
using namespace std;
#define NO_ADJ_DIG 13
typedef unsigned long long int big;
string n;
big getStartSum(int i){
big startSum = 1;
for(int k = i; k < i + NO_ADJ_DIG; k++){
startSum *= ( (int)(n[k] - '0'));
}
return startSum;
}
void start(){
big maxProduct = getStartSum(0);
// rest
big copyProduct = maxProduct;
int flag = -1;
for(int i = NO_ADJ_DIG; i< 1000; i++){
int newDigit = (int)(n[i] - '0');
int prevDigit = (int)(n[i - NO_ADJ_DIG] - '0');
if(newDigit == 0){
copyProduct = getStartSum(i+1);
i+= NO_ADJ_DIG;
continue;
}else{
copyProduct *= newDigit;
if(prevDigit != 0){
copyProduct /= prevDigit;
}
}
if(copyProduct > maxProduct){
maxProduct = copyProduct;
}
}
cout << maxProduct << endl;
}
int main(void){
string inputString;
while (getline(cin, inputString))
{
n+=inputString;
}
start();
return 0;
}