Hangzhou Electric OJ 1248 full knapsack problem undead Lich King Kong R & D program byte beating the original title

Reprinted to: https: //blog.csdn.net/ssdut_209/article/details/51557776 

 

Problem Description
undead Lich King wages pull, N Death Knight get a dollar bill (remember, only a bill), in order to prevent themselves frequently die in the battle, he decided to buy himself some of the props so he came to the Goblin Merchant.

Death Knight: "I want to buy props!"

Goblin Merchant: "Here we have three props, potions 150 a, 200 a magic drug, potion 350 a invincible."

Death Knight: "Okay, give me a potions."

Then he took out dollar bills handed goes N Goblin Merchant.

Goblin Merchant: "I forgot to remind you, we do not have the habit of looking for the guests money, and more money when we have received a tip, hey."

Death knight:"......"

Death Knight thought it would send a tip money when he himself might as well buy more props, anyway, later to be bought, bought breakfast at home or, as little as possible but to make him earn tips.

Death Knights now want you to help him calculate, how much he will give at least a tip businessman Goblin.
 

Input
first line of input data is an integer T (1 <= T <= 100), represents the number of test data then is T rows of test data, the test data each containing only a positive integer N (1 <= N < = 10000), N represents the face value of the death knight in the hands of bank notes.

Note: Goblin shops are only three items described in the title.
 

Output
For each test case, you have to waste a minimum output death knight how much money to the merchant as a fine tip.
 

Sample Input
2 900 250
 

Sample Output
0 50

code show as below:

 1 #include <iostream>
 2 using namespace std;
 3  
 4  
 5 int Max(int a,int b)
 6 {
 7     return a>b?a:b;
 8 }
 9 int main()
10 {
11     //freopen("D:\\input.txt","r",stdin);
12     //freopen("D:\\output.txt","w",stdout);
13     int cost[3]={150,200,350};
14     int value[3]={150,200,350};
15     int t;
16     int * v;
17     while(cin>>t)
18     {
19         while(t--)
20         {
21             int max=-1;
22             int n;
23             cin>>n;
24             v=new int [n+1];                //加一!!
25             if(n<150)
26             {
27                 cout<<n<<endl;;
28                 continue;
29             }
30             memset(v,0,sizeof(v));
31             for(int i=0;i<3;i++)
32             {
33                 for(int k=0;k<=n;k++)        //长度是n+1!
34                 {
35                     if(k>=cost[i])
36                     {
37                         v[k]=Max(v[k],v[k-cost[i]]+value[i]);
38                     //    cout<<"v["<<k<<"]"<<v[k]<<endl;
39                         if(v[k]>max)
40                             max=v[k];
41                     }
42                 }
43             }
44             delete v;
45             cout<<n-max<<endl;
46         }
47     }
48 }

 

Guess you like

Origin www.cnblogs.com/yichengming/p/11129908.html