hdu2048

15103758_9YO6.gif 15103758_ocKt.gif 带路径的数塔

    
#include " iostream "
using namespace std;
int main()
{
int a[ 100 ][ 100 ],b[ 100 ];
int i,j;
int n;
while (cin >> n)
{
for (i = 0 ;i < n;i ++ )
for (j = 0 ;j <= i;j ++ )
cin
>> a[i][j];
int t = 0 ;
int y = a[ 0 ][ 0 ];
for (i = n - 2 ;i >= 0 ;i -- )
{
int max = 0 ,k = 0 ;
for (j = 0 ;j <= i;j ++ )
{
if (a[i + 1 ][j] > a[i + 1 ][j + 1 ])//将每次出现的最大路径保存即可
{
a[i][j]
+= a[i + 1 ][j];
if (max < a[i][j]) { max = a[i][j]; k = a[i + 1 ][j]; b[t ++ ] = k;}
}
else {
a[i][j]
+= a[i + 1 ][j + 1 ];
if (max < a[i][j]) {max = a[i][j]; k = a[i + 1 ][j + 1 ]; b[t ++ ] = k;}

}
}
}
cout
<< a[ 0 ][ 0 ] << endl;
cout
<< y << " " ;
if (n % 2 == 0 )
{
for (i = t - 1 ;i >= 0 ;i -- ) //求路径之差
{
cout
<< b[i] - b[i - 1 ];
if (i != 0 ) cout << " " ;
}
}
else
{
for (i = t - 1 ;i >= 2 ;i -- )
{
cout
<< b[i] - b[i - 1 ];
if (i != 2 ) cout << " " ;
}
cout
<< " " << b[ 0 ];
}
// cout<<endl;

}
return 0 ;
}
15103758_9YO6.gif 15103758_ocKt.gif 数塔(典型)

    
1 #include " iostream "
2 #define M 100
3 using namespace std;
4 int Max( int a, int b)
5 {
6 return a > b ? a:b;
7 }
8 int main()
9 {
10 int m,n;
11 int i,j;
12 int dp[M][M];
13 cin >> m;
14 while (m -- )
15 {
16 cin >> n;
17
18 for (i = 0 ;i < n;i ++ )
19 for (j = 0 ;j <= i;j ++ )
20 cin >> dp[i][j];
21
22 for (i = n - 2 ;i >= 0 ;i -- )
23 {
24 for (j = 0 ;j < n;j ++ )
25 dp[i][j] += Max(dp[i + 1 ][j],dp[i + 1 ][j + 1 ]);
26 }
27 cout << dp[ 0 ][ 0 ] << endl;
28 }
29 return 0 ;
30 }

转载于:https://my.oschina.net/garyun/blog/602896

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值