[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>数字三角形
<>题目
<>7
<>3 8
<>8 1 0
<>2 7 4 4
<>4 5 2 6 5 (图一)
<>图一表示一个5行的数字三角形。假设给定一个n行数字三角形,计算出从三角形顶至底的一条路径,使该路径经过的数字总和最大。
每一步只能由当前位置向下或向右下。
<>输入
你的程序要能接受标准输入。第一行包含一个整数T,表示总的测试次数。
对于每一种情况:第一行包含一个整数N,其中1 < N < 100,表示三角形的行数。
接下来的N行输入表示三角形的每一行的元素Ai,j,其中0 < Ai,j < 100。
<>输出
输出每次测试的最大值并且占一行。
<>样例输入[复制](javascript:CopyToClipboard($(‘#sampleinput’).text()))
1 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
<>样例输出[复制](javascript:CopyToClipboard($(‘#sampleoutput’).text()))
30
<>解析
<>
这是一道难度二级的动态规划经典例题,在看到数字三角形的构造时,我们首先想到用二维数组来存放整个三角形(行与列),所求是路径最大数字和,也许有人会想到贪心算法来实现,但是贪心算法在这里是不适用的,因为凡事不能只看现在嘛,贪心中一次查找到大值,最后总值却不是,所以眼光放长,我们要的是最后的最大,来尝试用动态规划解决问题吧,用动态规划也有不同的思路和方法,首先我们的切入点是由下至上的回溯,依层次更换改动大值,回到顶端时,就是结果咯。
<>代码流程
#include <bits/stdc++.h> #include <iostream> #include <cmath> #include <ctime>
using namespace std; int main() { int T; cin >> T;//测试次数 while (T--) { int a;
cin >> a;//三角形行数 int z[a][a];//数字三角形二维数组 for (int i = 0; i < a; ++i) { for (int
j = 0; j <= i; ++j) { cin >> z[i][j]; } }//录入数字三角形 int dp[a][a]; for (int j =
0; j < a; ++j) { dp[a - 1][j] = z[a - 1][j]; }//把最后一行的值录到dp里去,后续对最后一行进行层层递上操作
for (int i = a - 2; i >= 0; i--) {//倒数第二行开始 for (int j = 0; j <= i; ++j) {
dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]) + z[i][j];//一个值加上成下方和右下方中最大的一个值
} } cout << dp[0][0] << endl; } }
<>手记草稿小流程(丑,但能看——)
44237439)(/Users/mac/Library/Containers/com.tencent.qq/Data/Library/Caches/Images/F7C641309F6ECF1F3CEE587AB6E624DC.jpg)]
<>总结
<>1.数学思想,发现规律
<>2.递推思想,操作效率
<>3.动态规划