# Digital triangle Problems

Source: Internet
Author: User

Digital triangle Problems

Memory limit: 65536kb time limit: 300 ms

Accept: 62 submit: 146

Description

Shows a digital triangle composed of N numbers. Design an algorithm to calculate a path from the top of the triangle to the bottom, so that the total number passing through the path is the largest.

7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

For a given number triangle composed of N rows of numbers, calculate the maximum value of the number and the path from the top to bottom of the triangle.

Input

The first line of the input is the number of N, 1 <= n <= 600. The next n rows are numbers in each row of a digital triangle. All numbers are in the range of 0 .. 99.

Output

Output: the maximum value.

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample output

`#include<iostream>``#include<stdio.h>``#include<stdlib.h>``#include<stdlib.h>``using``namespace``std ;``int` `Data;``int` `M;``int` `main()``{``    ``int``T = 0 ;``    ``scanf``(``"%d"``,&T) ;``    ``int``k = 1 ;``    ``int``i = 1 ;``    ``int``j = 1 ;``    ``memset``(M,0,``sizeof``(M));``    ``while``(k<=T)``    ``{``        ``for``(j=1;j<=k;j++)``        ``{``            ``scanf``(``"%d"``,&Data[k][j]);``            ``//cout<<"\nData: i: "<<k<<" J:"<<j <<" "<<Data[k][j]<<" ";``        ``}``        ``k++;``    ``}``    ``for``( i =T ; i >=1 ; i -- )``        ``for``(j =1 ; j <= i ; j++ )``        ``{` `            ``if``( i ==T )``            ``{``                ``M[i][j] = Data[i][j];``            ``}``            ``else``{``                ``int``a = M[i+1][j] +Data[i][j];``                ``int``b = M[i+1][j+1] +Data[i][j];``                ``a>b ? M[i][j] = a : M[i][j] = b ;``            ``}``        ``//  cout<<"\nM: i: "<<i<<" J:"<<j <<" "<<M[i][j]<<" ";``        ``}``    ` `    ``cout<<M<<endl;``    ``return``0 ;``}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.