summaryrefslogtreecommitdiff
path: root/euler18.c
diff options
context:
space:
mode:
authorIru Cai <mytbk920423@gmail.com>2018-05-24 21:39:58 +0800
committerIru Cai <mytbk920423@gmail.com>2018-05-24 21:39:58 +0800
commit1eefd58ca4fdb5d2f51f657bfd70c9a89a4707db (patch)
treeabde0e4da3c7fe138f3874a94d8eb7d0e44c3224 /euler18.c
downloadproject_euler-1eefd58ca4fdb5d2f51f657bfd70c9a89a4707db.tar.xz
initial commit
Diffstat (limited to 'euler18.c')
-rw-r--r--euler18.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/euler18.c b/euler18.c
new file mode 100644
index 0000000..281dc77
--- /dev/null
+++ b/euler18.c
@@ -0,0 +1,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;
+}
+
+