拿到这一道题 上去就开始了 贪心 , 但是贪心是一种概率上最优解 , 不是真正的最优解 , 所以在这里就错了 这一道题很明显可以用01背包去解决 . 这里附上 01背包的解决办法.
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 #include<vector> 8 #include<set> 9 #include<stack> 10 #include<string> 11 #include<sstream> 12 #include<map> 13 #include<cctype> 14 #include<limits.h> 15 using namespace std; 16 int main() 17 { 18 int t,total,n,worth[25],Importance_degree[25],result[30005]; 19 scanf("%d",&t); 20 while(t--) 21 { 22 scanf("%d%d",&total,&n); 23 memset(result,0,sizeof(result)); 24 memset(worth,0,sizeof(worth)); 25 memset(Importance_degree,0,sizeof(Importance_degree)); 26 for(int i=0;i<n;i++) 27 scanf("%d%d",&worth[i],&Importance_degree[i]); 28 for(int i=0;i<n;i++) 29 { 30 for(int v=total;t>=0;v--) 31 { 32 if(v<worth[i]) 33 break; 34 result[v]=max(result[v],result[v-worth[i]]+worth[i]*Importance_degree[i]); 35 } 36 } 37 printf("%d\n",result[total]); 38 } 39 return 0; 40 }