一尾江河 @ 2017-05-21 11:55:13
唔代码如下
#include <iostream>
#include <algorithm>
using namespace std;
int d[1001][1001];
int n;
int maxsum[1001][1001];
int dp(int i,int j){
if(maxsum[i][j]!=1) return maxsum[i][j];
if(i==n) maxsum[i][j]=d[i][j];
else{
int x=dp(i+1,j);
int y=dp(i+1,j+1);
maxsum[i][j]=max(x,y)+d[i][j];
}
return maxsum[i][j];
}
int main(){
int i,j;
cin>>n;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
cin>>d[i][j];
maxsum[i][j]=1;
}
}
cout<<dp(1,1)<<endl;
return 0;
}
by 1124828077ccj @ 2017-05-21 12:05:16
@一尾江河 初始值不要设为1
by 一尾江河 @ 2017-05-21 14:35:36
@2016陈常杰
可我只是标记了一下啊
by 1124828077ccj @ 2017-05-21 21:35:01
@一尾江河 万一答案碰巧也是1呢?
by 1124828077ccj @ 2017-05-21 21:36:27
@一尾江河 我改了一下,把1改成-1就AC了
by 一尾江河 @ 2017-05-24 08:19:53
@2016陈常杰 O(∩_∩)O谢谢