C++01背包-装箱问题

这篇博客重点讨论C++如何解决经典的01背包问题,强调这是一个基础且重要的题目。内容包括直接展示解题思路和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

水题,也是基础题(炒鸡重要!~~~)

.
.小游戏——>>>2048—->>>flappy bird
.—>只是题意(只有聪明人看得见,啦啦啦啦啦)
.
.

装箱问题,题意我就不说啦,直接上思路+代码

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int a[31];
bool f[21000];    // 判断是否被填(基础的背包) 
int main()
{
    
### 贪心算法实现一维装箱问题 贪心算法是一种常见的解决装箱问题的方法,其基本思想是将物品依次放入当前最适合的箱子中,以尽量减少使用的箱子数量。以下是一个使用贪心算法实现的一维装箱问题C++ 示例代码: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; // 比较函数,用于降序排序 bool compareDesc(int a, int b) { return a > b; } // 贪心装箱算法 int greedyPacking(vector<int>& items, int binCapacity) { sort(items.begin(), items.end(), compareDesc); // 将物品按大小降序排序 vector<int> bins; // 存储每个箱子的剩余容量 for (int item : items) { bool placed = false; // 遍历所有箱子,寻找可以放置当前物品的箱子 for (int& bin : bins) { if (bin >= item) { bin -= item; placed = true; break; } } // 如果没有找到合适的箱子,则新开一个箱子 if (!placed) { bins.push_back(binCapacity - item); } } return bins.size(); // 返回使用的箱子数量 } int main() { vector<int> items = {48, 35, 32, 21, 17, 12, 8}; // 物品大小 int binCapacity = 50; // 箱子容量 int numBins = greedyPacking(items, binCapacity); cout << "需要的箱子数量: " << numBins << endl; return 0; } ``` ### 动态规划实现装箱问题 动态规划方法可以用于解决某些特定的装箱问题,例如背包问题。以下是一个使用动态规划实现的装箱问题示例,目标是将物品放入一个箱子中,使得箱子的剩余空间最小: ```cpp #include <iostream> #include <vector> using namespace std; // 动态规划实现装箱问题 int dpPacking(int binCapacity, vector<int>& items) { int n = items.size(); vector<vector<int>> dp(n + 1, vector<int>(binCapacity + 1, 0)); for (int i = 1; i <= n; ++i) { for (int j = 0; j <= binCapacity; ++j) { if (items[i - 1] > j) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - items[i - 1]] + items[i - 1]); } } } return binCapacity - dp[n][binCapacity]; // 返回箱子的剩余空间 } int main() { vector<int> items = {48, 35, 32, 21, 17, 12, 8}; // 物品大小 int binCapacity = 100; // 箱子容量 int remainingSpace = dpPacking(binCapacity, items); cout << "箱子的剩余空间: " << remainingSpace << endl; return 0; } ``` ### 箱子利用率优化 为了进一步优化箱子的利用率,可以采用首次适应下降(First Fit Decreasing, FFD)算法。该算法首先将物品按大小降序排序,然后使用首次适应(First Fit)算法将物品放入箱子中。首次适应算法的基本思想是将物品放入第一个可以容纳它的箱子中。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; // 比较函数,用于降序排序 bool compareDesc(int a, int b) { return a > b; } // 首次适应下降算法 int ffdPacking(vector<int>& items, int binCapacity) { sort(items.begin(), items.end(), compareDesc); // 将物品按大小降序排序 vector<int> bins; // 存储每个箱子的剩余容量 for (int item : items) { bool placed = false; // 遍历所有箱子,寻找可以放置当前物品的箱子 for (int& bin : bins) { if (bin >= item) { bin -= item; placed = true; break; } } // 如果没有找到合适的箱子,则新开一个箱子 if (!placed) { bins.push_back(binCapacity - item); } } return bins.size(); // 返回使用的箱子数量 } int main() { vector<int> items = {48, 35, 32, 21, 17, 12, 8}; // 物品大小 int binCapacity = 50; // 箱子容量 int numBins = ffdPacking(items, binCapacity); cout << "需要的箱子数量: " << numBins << endl; return 0; } ``` ### 装箱问题的其他优化方法 除了贪心算法和动态规划,还可以使用其他优化方法来解决装箱问题,例如遗传算法、模拟退火等。这些方法通常用于解决大规模装箱问题,能够获得更好的优化效果,但实现复杂度较高。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值