UV 624 CD (01 backpack), uva624

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.