-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-12_problem2.c
51 lines (48 loc) · 1.57 KB
/
01-12_problem2.c
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
#include <stdio.h>
#include <stdlib.h>
#define BufferSize 512
#define MAX_SIZE 100
#define REQUESTED_VALUES 3
/*See the problem 1 for easier optimization*/
int main(){
FILE *p;
char string[BufferSize];
int num[MAX_SIZE], i = 0, value, j, sum, maxValue[REQUESTED_VALUES], k = 0;
p = fopen("01-12_input.txt", "r");
maxValue[0] = 0;
maxValue[1] = 0;
maxValue[2] = 0;
while (!feof(p)){
value = 0;
fgets(string,BufferSize, p);
sscanf(string, "%d", &value);
num[i] = value;
i++;
if(value == 0){
sum = 0;
for(j = 0; j < i; j++){
sum += num[j];
}
i = 0;
k++;
/*printf("SUM:%d, %d, %d, %d\n", sum, maxValue[2], maxValue[1], maxValue[0]);*/
if (maxValue[2] < sum){
maxValue[0] = maxValue[1];
maxValue[1] = maxValue[2];
maxValue[2] = sum;
/*printf("%d, %d, %d\n", maxValue[2], maxValue[1], maxValue[0]);*/
}else if (maxValue[1] < sum){
maxValue[0] = maxValue[1];
maxValue[1] = sum;
/*printf("%d, %d, %d\n", maxValue[2], maxValue[1], maxValue[0]);*/
}else if (maxValue[0] < sum){
maxValue[0] = sum;
/*printf("%d, %d, %d\n", maxValue[2], maxValue[1], maxValue[0]);*/
}
}
}
fclose(p);
sum = maxValue[2] + maxValue[1] + maxValue[0];
printf("%d\n", sum);
return 0;
}