hdu 2602 bit 1013

01背包,具体不说,背包九讲写的真好。因为V_all这个数组一不小心在mian函数里面也定义了一个,结果导致本地测试时结果一直为零。大哭


#include <stdio.h>
#include <string.h>
int V_all[1100];
void oneZero(int N,int V,int *V_each,int *value_each){
	for(int i = 0;i<N;++i){
		for(int j = V;j >=V_each[i];--j){
			if(V_all[j] < V_all[j - V_each[i]] + value_each[i]){
				V_all[j] = V_all[j - V_each[i]] + value_each[i];
			}
		}
	}
} 


int main(int argc, char *argv[])
{
	//FILE *fp;	
	//fp = freopen("in2.txt","r",stdin);
	
	int test_case;
	int N,V;
	int value_each[1100],cost_each[1100];
	scanf("%d",&test_case);
	while(test_case --){
		scanf("%d%d",&N,&V);
		for(int i = 0;i<N;++i){
			scanf("%d",&value_each[i]);
		}	
		for(int i = 0;i<N;++i){
			scanf("%d",&cost_each[i]);
		}
		memset(V_all,0,sizeof(V_all));
		oneZero(N,V,cost_each,value_each);
		printf("%d\n",V_all[V]);
	}
	
	return 0;
}


你可能感兴趣的:(hdu 2602 bit 1013)