Description
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame.
Input
Output
Sample Input
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
Sample Output
30
Hint
7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5The highest score is achievable by traversing the cows as shown above.
此乃大水题,直接用贪心,每个点记录到这个点的最大值,最后遍历最后一层找出最大值即可。
#include <iostream> #include <stdio.h> using namespace std; int ma[355][355];int n; int kase;int a[355]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=i;j++) scanf("%d",&ma[i][j]); for(int i=2;i<=n;i++) { for(int j=1;j<=i;j++) ma[i][j]+=max(ma[i-1][j-1],ma[i-1][j]); } for(int i=1;i<=n;i++) { kase=max(kase,ma[n][i]); } cout<<kase<<endl; }