数塔问题
作者:互联网
#include<iostream> #include<algorithm> #include<string> using namespace std; int myMax(int a,int b){ return a>b?a:b; } void print(int a[][100],int n) { for(int i=1;i<=n;i++) { for(int j=(n-i);j>=1;j--) cout << " "; for(int j=1;j<=i;j++) cout << a[i][j] << " "; cout << endl; } } int func(int a[][100],int n) { int dp[100][100]; for(int i=1;i<=n;i++) dp[n][i]=a[n][i];//初始化 for(int i=n-1;i>=1;i--) for(int j=1;j<=i;j++) dp[i][j]=a[i][j]+myMax(dp[i+1][j],dp[i+1][j+1]); print(dp,n); return dp[1][1]; } int main(void) { freopen("D:\\1.txt","r",stdin); int a[100][100],n; cin >> n; for(int i=1;i<=n;i++) for(int j=1;j<=i;j++) cin >> a[i][j]; print(a,n); int ans = func(a,n); cout << "ans = " << ans; return 0; }
标签:std,return,cout,数塔,int,问题,print,include 来源: https://www.cnblogs.com/zuimeiyujianni/p/10461670.html