动态规划 <pre name="code" class="cpp">f[i][j]=a[i][j]+max(f[i+1][j],f[i+1][j+1]); #include <iostream> #include<cstring> #include <cmath> #include <algorithm> using namespace std; int f[102][102]; int a[102][102]; int n; int dfs(int i,int j) { if (f[i][j]>=0) { return f[i][j]; } if (i==n-1) { f[i][j]=a[i][j]; return f[i][j]; } f[i][j]=a[i][j]+max(dfs(i+1,j),dfs(i+1,j+1)); return f[i][j]; } int main() { cin>>n; memset(f,-1,sizeof(f)); for(int i=0;i<n;i++) { for (int j=0;j<=i;j++) { cin>>a[i][j]; } } cout<<dfs(0,0)<<endl; return 0; }