01背包问题c语言代码还有注释要求能输入数据
时间: 2023-08-12 11:23:32 浏览: 80
好的,下面是一个使用动态规划算法解决0/1背包问题的C语言代码,包含详细的注释。同时,代码中也包含了输入数据的部分,可以根据需要自行修改。
```c
#include <stdio.h>
#include <stdlib.h>
// 定义背包物品的结构体
struct item {
int weight; // 物品重量
int value; // 物品价值
};
// 定义动态规划表格的结构体
struct dp_table {
int max_value; // 当前背包容量下的最大价值
int item_num; // 当前背包容量下最优解中所选物品个数
};
// 动态规划函数,返回背包能够容纳的最大价值
int knapsack(int capacity, struct item *items, int item_num, struct dp_table **dp)
{
// 分配动态规划表格的内存空间
*dp = (struct dp_table *)malloc((item_num + 1) * sizeof(struct dp_table));
for (int i = 0; i <= item_num; i++) {
(*dp)[i].max_value = 0;
(*dp)[i].item_num = 0;
}
// 填充动态规划表格
for (int i = 1; i <= item_num; i++) {
for (int j = 0; j <= capacity; j++) {
if (j < items[i-1].weight) {
// 当前物品无法放入背包,继承上一个状态的最大价值
(*dp)[i].max_value = (*dp)[i-1].max_value;
(*dp)[i].item_num = (*dp)[i-1].item_num;
} else {
// 当前物品可以放入背包,比较上一个状态和当前状态的最大价值
int prev_value = (*dp)[i-1].max_value;
int curr_value = (*dp)[i-1].max_value + items[i-1].value;
if (curr_value > prev_value) {
(*dp)[i].max_value = curr_value;
(*dp)[i].item_num = (*dp)[i-1].item_num + 1;
} else {
(*dp)[i].max_value = prev_value;
(*dp)[i].item_num = (*dp)[i-1].item_num;
}
}
}
}
// 返回背包能够容纳的最大价值
return (*dp)[item_num].max_value;
}
int main()
{
int capacity; // 背包容量
printf("请输入背包容量:");
scanf("%d", &capacity);
int item_num; // 物品数量
printf("请输入物品数量:");
scanf("%d", &item_num);
// 分配物品数组的内存空间
struct item *items = (struct item *)malloc(item_num * sizeof(struct item));
// 输入物品的重量和价值
for (int i = 0; i < item_num; i++) {
printf("请输入第%d个物品的重量和价值:", i+1);
scanf("%d %d", &items[i].weight, &items[i].value);
}
struct dp_table *dp; // 动态规划表格
int max_value = knapsack(capacity, items, item_num, &dp); // 求解背包问题
// 输出最大价值和所选物品个数
printf("背包能够容纳的最大价值为:%d\n", max_value);
printf("所选物品个数为:%d\n", dp[item_num].item_num);
// 释放内存空间
free(items);
free(dp);
return 0;
}
```
希望能对你有所帮助!
阅读全文
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)