Apple time limit:MS | Memory limit:65535 KB Difficulty:3
-
Describe
-
CTest has n apples to put into a backpack with a capacity of V. Give the size and price of the first Apple, and find out the total price of the apples that can be put into the backpack.
-
-
Input
-
-
There are several sets of test data, the first behavior of each group of test data 2 positive integers, respectively, the number of apples is n and the capacity of the backpack V,n, v at the same time to end the test at 0 o'clock, not output. The next n lines, 2 positive integers per line, are separated by a space, representing the size C and the price W of the apples, respectively. The range of all input numbers is greater than or equal to 0, less than or equal to 1000.
-
-
Output
-
-
output An integer for each set of test data, representing the total value of the apple that can be put into the backpack.
-
-
Sample input
-
-
3 31 12 13 10 0
-
-
Sample output
-
2
1 2#include <iostream>3 using namespacestd;4#include <algorithm>5 intc[1010],w[1010];//c: Size, W: Price;6 intf[1010][1010];7 intMain ()8 {9 intN,v,i,j;//N: Number of apples, V: Backpack capacityTen while(cin>>n>>v,n| |v) One { A for(i=1; i<=n;i++) -Cin>>c[i]>>W[i]; - for(i=1; i<=n;i++) the for(j=0; j<=v;j++) - { -f[i][j]=f[i-1][j]; - if(j>=C[i]) +F[i][j]=max (f[i-1][j],f[i-1][j-c[i]]+w[i]); - } +cout<<f[n][v]<<Endl; A } at return 0; - } -
1#include <iostream>2#include <string.h>3 using namespacestd;4 intf[1010];5 intMain ()6 {7 intN,v,i,j,c,w;//N: Number of apples, V: Backpack capacity C: Size, W: Price;8 while(cin>>n>>v,n| |v)9 {Ten intf[1010]; OneMemset (F,0,sizeof(f)); A for(i=1; i<=n;i++) - { -Cin>>c>>W; the for(j=v;j>=c;j--) - { - if(f[j]<f[j-c]+W) -f[j]=f[j-c]+W; + } - } +cout<<f[v]<<Endl; A } at return 0; -}
Nyoj 289 Apples (01 backpacks)