从原点(顶层)出发,只能向左或者向右,找到一条路径使得路径上的数字和最大:
#include<stdio.h>
//#include"algorithm.h"
#define N 100int max(int a, int b)
{return a > b ? a : b;
}int main()
{int a[N][N], i, j;int n = 5;//scanf("%d", &n);for (i = 0; i < n; i++){for (j = 0; j <= i; j++){scanf("%d", &a[i][j]);}}//上塔printf("坐标:\n");for (i = n - 2; i >= 0; i--){for (j = i; j >= 0; j--){a[i][j] += max(a[i + 1][j], a[i + 1][j + 1]);//当前值加上子级中的较大值printf("%d %d %d\n", i + 1, j + 1, a[i][j]);}printf("\n");}
}