多重背包

多重背包

 

Weight

Value

Amount

A

2

3

3

B

1

2

2

C

3

4

1

D

2

2

2

问题:有n种物品,每种物品有amount[i]个,每个物品的重量为weight[i],每个物品的价值为value[i].现在有一个背包,他所能容纳的容量为total,问:当你面对多么有价值的物品时,你的背包所能带走的最大价值是多少?

 

amoun

0

1

2

3

4

5

A

3

0

0

3

3

6

6

B

2

0

2

4

3

6

6

C

1

0

2

4

4

6

8

D

2

0

2

4

4

6

8

 

 

 

 

 

 

 

 

 

 

0

1

2

3

4

5

A

0

0

3

3

6

6

B

0

2

4

6

8

10

C

0

2

4

6

8

10

D

0

2

4

6

8

10

#include <stdio.h>
#include <string.h> 
int max(int a,int b){
	if(a>b)
	return a;
	else
	return b;
}
int main(){
	int goods,i,weight[100],value[100],amount[100],rad[100],j,count[100],capacity;
	printf("please imput the number of the goods:\n");
	scanf("%d",&goods);
	printf("please imput the weight and the value and the amount of the goods:\n");
	for(i=1;i<=goods;i++){
		scanf("%d%d%d",&weight[i],&value[i],&amount[i]);
	}
	scanf("%d",&capacity);
	memset(rad,0,sizeof(rad));
	
	for(j=1;j<=goods;j++){
		for(i=0;i<=capacity;i++){
			count[i]=0;
			if(i==0){
				rad[i]=0;
				printf("%d ",rad[i]);
			}
			else if(i<weight[j]){
				printf("%d ",rad[i]);
			} 
			else{
				if(count[i-weight[j]]<amount[j]){
					if(rad[i-weight[j]]+value[j]>rad[i]){
						rad[i]=rad[i-weight[j]]+value[j];
						printf("%d ",rad[i]);
						count[i]=count[i-weight[j]]+1; 
					}
					else{
						printf("%d ",rad[i]);
					}
				}
				else{
					printf("%d ",rad[i]);
				}
			} 
		}
		printf("\n");
	}
	printf("%d\n",rad[capacity]);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值