苹果(背包)

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<string.h>
 4 using namespace std;
 5 int bag[1001];
 6 struct Case{
 7     int c,w;
 8 }apple[1010];
 9 /*int cmp(Case a,Case b){
10     return (1.0*a.w/a.c)>(1.0*b.w/b.c);
11 }*/
12 int main(){
13     int n,v;
14     while(scanf("%d%d",&n,&v),n||v){memset(bag,0,sizeof(bag));
15         for(int i=0;i<n;++i)scanf("%d%d",&apple[i].c,&apple[i].w);
16         /*sort(apple,apple+n,cmp);*/
17         for(int i=0;i<n;++i){ //printf("%d ",apple[i].c);
18             for(int j=v;j>=apple[i].c;j--)if(bag[j-apple[i].c]+apple[i].w>bag[j])bag[j]=bag[j-apple[i].c]+apple[i].w;
19         }
20         printf("%d\n",bag[v]);
21     }
22     return 0;
23 }

 

posted @ 2015-05-25 23:11  handsomecui  阅读(344)  评论(0编辑  收藏  举报