Question summary-digital triangle

Question summary-digital triangle

topic

Digital triangle title description
Digital triangle input and output

Problem analysis:

This question is to find the route with the largest value from top to bottom (very similar to the "Yang Hui Triangle")

Question ideas:

This problem is definitely wrong to use the greedy algorithm!

First introduce a solution. This question can use the "Yang Hui triangle" idea, according to a recursive formula of the upper element and the lower two elements (called the state transition equation in dynamic programming), solve this problem from the bottom to the top (the detailed ideas will be discussed later) Make up)

Code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int n,a[1010][1010],i,j;      //这里地二维数组空间一定要开的足够大
	cin>>n;
	for(i=1;i<=n;i++)
	{
    
    
		for(j=1;j<=i;j++)
		{
    
    
			cin>>a[i][j];
		}
	}
	/*if(n==1)            
	{
		cout<<a[1][1]<<endl;
		return 0;
	}*/
	for(i=n-1;i>=1;i--)              //按照递推公式(状态转移方程)进行实现
	{
    
    
		for(j=1;j<=i;j++)
		{
    
    
			a[i][j]+=max(a[i+1][j],a[i+1][j+1]);
		}
	}
	cout<<a[1][1]<<endl;
}

Guess you like

Origin blog.csdn.net/m0_46772594/article/details/108113550