A Dynamic Digital triangle |
Difficulty level: B; run time limit: 1000ms; operating space limit: 51200KB; code length limit: 2000000B |
Question Description |
A number consists of a triangle with n rows and the number of I in line I. Starting with the first number, you can go down one block at a time, down to the left or right, until you reach the last line, adding up the number of passes along the way. How to walk to get the biggest and? As an example:
For the sake of simplicity, enter the number of each line sequentially, and do not enter a space before the first number. |
Input |
The first line: N, which indicates that the triangle has n rows Second to n+1: the data for each row of the number triangle, entered sequentially, is separated by a space between two numbers. |
Output |
A number that represents the largest and |
Input example |
4 1 3 2 4 10 1 4 3 2 20 |
Output example |
24 |
1#include <iostream>2#include <algorithm>3 using namespacestd;4 #defineMAXN 10005 intN,I,J,A[MAXN][MAXN];6 intMain ()7 {8scanf"%d",&n);9 for(i=1; i<=n;i++)Ten for(j=1; j<=i;j++) scanf ("%d",&a[i][j]); One for(i=n-1; i>=1; i--) A for(j=1; j<=i;j++) A[i][j]=max (a[i][j]+a[i+1][j],a[i][j]+a[i+1][j+1]); -printf"%d\n", a[1][1]); - return 0; the}
View Code
A simple dynamic programming---a dynamic digital triangle