UV 624 CD (01 backpack), uva624
[Question link]: click here ~~
Code:
/** Problem: VA No. 624 * Running time: 0 MS * Complier: C ++ * Author: javaherongwei * Create Time:, Tuesday, seeking the recording sequence whose sum is not greater than N [Train of Thought] requires the sum to be closest to N. You can use the 01 backpack idea to regard different tracks as the size of a backpack, to solve this problem, you can convert it to the maximum value of N. You can use a two-dimensional array to record the selected number */# include <iostream> # include <bits/stdc ++. h> using namespace std; const int N = 10005; int volume [N]; int dp [N]; bool vis [25] [N]; int main () {int n, V; while (scanf ("% d ", & V, & n )! = EOF) {memset (volume, 0, sizeof (volume); // input memset (dp, 0, sizeof (dp); memset (vis, 0, sizeof (vis); // vis true or not for (int I = 0; I <n; ++ I) {scanf ("% d ", & volume [I]) ;}for (int I = 0; I <n; ++ I) {for (int v = V; v> = volume [I]; -- v) {if (dp [v] <= dp [v-volume [I] + volume [I]) //! = {Dp [v] = dp [v-volume [I] + volume [I]; vis [I] [v] = 1; // vis true }}for (int I = n-1, j = V; I> = 0; I --) {if (vis [I] [j]) // if vis true, print the number {printf ("% d", volume [I]); j-= volume [I] ;}} printf ("sum: % d \ n ", dp [V]);} return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.