blob: 7cb961eede6095afda2fd0404c4bf14cc8c84866 (
plain)
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
73
74
75
|
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#if 0
#define FILENAME "sample.txt"
#else
#define FILENAME "input.txt"
#endif
int biggest[3];
int bigger(int val)
{
for(int i = 2; i >= 0; i--)
if(val > biggest[i]) return i;
return -1;
}
void shift(int n, int val)
{
for(int i = 1; i <= n; i++)
biggest[i-1] = biggest[i];
biggest[n] = val;
}
void check(int val)
{
int b = bigger(val);
if(b != -1)
shift(b, val);
}
void parse()
{
FILE *fp = fopen(FILENAME, "r");
if(!fp) {
fprintf(stderr, "ERROR: Could not open file %s\n", FILENAME);
exit(1);
}
char line[256];
int sum = 0;
while(fgets(line, sizeof(line), fp) != NULL)
{
if(line[0] == '\n') {
check(sum);
sum = 0;
}
sum += atoi(line);
}
check(sum); // the file does not end in a newline
fclose(fp);
}
int main(void)
{
for(int i = 0; i < 3; i++)
biggest[i] = INT_MIN;
parse();
int sum = 0;
for(int i = 0; i < 3; i++) {
printf("%d\n", biggest[i]);
sum += biggest[i];
}
printf("\n%d\n", sum);
return 0;
}
|