summaryrefslogtreecommitdiff
path: root/euler18.c
blob: 281dc775dec8fb21b237ebd6b467735716c6f2c1 (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
#include <stdio.h>
#define MAX 15

int max(int i,int j)
{
	if (i>j)
		return i;
	else
		return j;
}

int main()
{
	int triangle[MAX][MAX];
	int answer[MAX][MAX];
	int maxtotal=0;
	int i,j;
	for (i=0; i<MAX; ++i){
		for (j=0; j<=i; ++j){
			scanf("%d",&triangle[i][j]);
			if (i==0){
				answer[i][j]=triangle[i][j];
			}else if (j==0){
				answer[i][j]=
					triangle[i][j]+answer[i-1][j];
			}else if (j==i){
				answer[i][j]=
					triangle[i][j]+answer[i-1][j-1];
			}else{
				answer[i][j]=
					triangle[i][j]+
					max(answer[i-1][j-1],answer[i-1][j]);
			}
			if (answer[i][j]>maxtotal){
				maxtotal=answer[i][j];
			}
		}
	}
	printf("%d\n",maxtotal);
	return 0;
}