1 Star 0 Fork 0

白衣渡江/leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
01dp.cc 839 Bytes
一键复制 编辑 原始数据 按行查看 历史
白衣渡江 提交于 2024-04-03 04:10 . 01背包
#include <iostream>
#include <vector>
using std::vector;
using std::endl;
using std::cout;
using std::cin;
int n,bagweight;
void solve(){
vector<int> weight(n,0);
vector<int> value(n,0);
for(int i = 0;i<n;i++){
cin>>weight[i];
}
for(int j = 0;j<n;j++){
cin>>value[j];
}
vector<vector<int>> dp(weight.size(),vector<int>(bagweight+1,0));//初始化dp数组
for(int j = weight[0];j<=bagweight;j++){
dp[0][j] = value[0];
}
for(size_t i = 1;i<weight.size();i++){
for(int j = 0;j<=bagweight;j++){
if(j <weight[i]){
dp[i][j] = dp[i-1][j];
}else{
dp[i][j] = std::max(dp[i-1][j],dp[i-1][j-weight[i]]+value[i]);
}
}
}
}
int main(){
while(cin>>n>>bagweight){
solve();
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/zheng-jianlin/leetcode.git
git@gitee.com:zheng-jianlin/leetcode.git
zheng-jianlin
leetcode
leetcode
master

搜索帮助