[{"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}]
<>最少硬币问题
<>题目
<>设有 n 种不同面值的硬币,各硬币的面值存于数组 T[1:n]中。现要用这些面值的硬币来找钱。可以使用的各种面值的硬币个数存于数组
Coins[1:n]中。对任意钱数 0≤m≤20001,设计一个用最少硬币找钱 m 的方法。
<>算法设计:
对于给定的 1≤n≤10,硬币面值数组 T 和可以使用的各种面值的硬币个数数组 Coins,以及钱数 m,0≤m≤20001,计算找钱 m 的最少硬币数。
<>输入
<>第一行中只有 1 个整数给出n 的值,第 2 行起每行 2 个数,分别是 T[j]和 Coins[j]。最后 1 行是要找的钱数 m。
<>输出
<>将计算出的最少硬币数输出. 如果不能满足所需要求输出-1
<>样例输入[复制](javascript:CopyToClipboard($(‘#sampleinput’).text()))
3 1 3 2 3 5 3 18
<>样例输出[复制](javascript:CopyToClipboard($(‘#sampleoutput’).text()))
5
<>解析
<>这是一道难度三级的动态规划经典例题,实现动态规划要考虑三个方面,
<>第一是最优化原理;
<>第二是无后效性(即某阶段状态一旦确定,就不受这个状态以后决策的影响。也就是说,某状态以后的过程不会影响以前的状态,只与当前状态有关);
<>
第三是有重叠子问题(即子问题之间是不独立的,一个子问题在下一阶段决策中可能被多次使用到(该性质并不是动态规划适用的必要条件,但是如果没有这条性质,动态规划算法同其他算法相比就不具备优势))。
<>使用一维数组来存储结果dp[20002],循环中注意边界
<>动态迁移方程为
<>dp[k] = min(dp[k - T[i]] + 1, dp[k])
<>代码流程
#include <bits/stdc++.h> using namespace std; int main() { int n, m,
dp[20002]; cin >> n; int T[n + 1], Coins[n + 1]; for (int i = 1; i <= n; i++)
cin >> T[i] >> Coins[i]; cin >> m;//成功输入硬币种类,分别的面值与数量及最后找钱m for (int i = 1; i <
20002; i++) dp[i] = 1000000;//一维数组存储结果初定义 dp[0] = 0; for (int i = 1; i <= n;
i++)//面值种类 for (int j = 1; j <= Coins[i]; j++)//面值数量 for (int k = m; k >= T[i];
k--) dp[k] = min(dp[k - T[i]] + 1, dp[k]);//核心(先找面值大的硬币,) cout << (dp[m] <
1000000 ? dp[m] : -1) << endl;//问题无解输出-1 return 0; }
<>复杂度分析
<>时间复杂度(nl)
<>空间复杂度(l)
<>总结
<>设计动态规划第一步就是要刻画好最优子结构,以自底向上从子问题的最优解构造出整个问题的最优解。