问题描述:背包8KG,如何装总价最多的物品。
解决:动态规划 有1~8KG负重的背包各一个,依次求每一个的最佳解,最后8KG的即为题目所要求的。
水果依次a[0] a[1] a[2] a[3] a[4] 第三行记录下标
①放李子a[0]:4KG,$4500
1KG 2KG 3KG 4KG 5KG 6KG 7KG 8KG
0 0 0 4500 4500 4500 4500 9000
- - - 0号 0号 0号 0号 0号*2
②放苹果a[1]:5KG,$5700
1KG 2KG 3KG 4KG 5KG 6KG 7KG 8KG
0 0 0 4500 5700 5700 5700 9000
- - - 0号 1号 1号 1号 0号
1KG 2KG 3KG 4KG 5KG 6KG 7KG 8KG
0 2250 2250 4500 5700 6750 7950 9000
- 2号 2号 0号 1号 2号 2号 0号
(4KG:2*2250<4500 ∴4500不变
6KG:4500+2250=6750>5700
7KG:5700+2250=7950>5700
8KG:6750+2250<9000 ∴9000不变)
④放草莓a[3]:1KG,$1100
1KG 2KG 3KG 4KG 5KG 6KG 7KG 8KG
1100 2250 3350 4500 5700 6800 7950 9050
3号 2号 1号 0号 1号 3号 2号 3号
1KG 2KG 3KG 4KG 5KG 6KG 7KG 8KG
1100 2250 3350 4500 5700 6800 7950 9050
3号 2号 1号 0号 1号 3号 2号 3号
结束。
8KG:先装a[3]草莓。
8-1=7KG,看7KG的包:装a[2]橘子。
7-2=5KG,看5KG的包:装a[1]苹果。
OK
代码:
#include <stdio.h>
#include <stdlib.h>
#define LIMIT 8 // 重量限制
#define N 5 // 物品种类
#define MIN 1 // 最小重量
struct body
{
char name[20];
int size;
int price;
};
typedef struct body object;
int main(void)
{
int item[LIMIT+1] = {0};
int value[LIMIT+1] = {0};
int newvalue, i, s, p;
object a[] = {
{"李子", 4, 4500}, // 物品名字 重量 单价
{"苹果", 5, 5700},
{"橘子", 2, 2250},
{"草莓", 1, 1100},
{"甜瓜", 6, 6700}
};
for(i = 0; i < N; i++)
{
for(s = a[i].size; s <= LIMIT; s++)
{
p = s - a[i].size;
newvalue = value[p] + a[i].price;
if(newvalue > value[s])
{
// 找到阶段最佳解
value[s] = newvalue;
item[s] = i;
}
}
}
printf("物品\t价格\n");
for(i = LIMIT; i >= MIN; i = i - a[item[i]].size)
{
printf("%s\t%d\n",
a[item[i]].name, a[item[i]].price);
}
printf("合计\t%d\n", value[LIMIT]);
return 0;
}