Programming algorithms-Multiple parts and problem codes (C)

Source: Internet
Author: User

multiple parts and problem codes (C)


This address: Http://blog.csdn.net/caroline_wendy


Title: There are n different sizes of numbers a, each of each m. The inference can be enough to select some of these numbers to make their and exactly K.


Using dynamic Solver (DP),

Method 1: dp[i+1][j] = The number of the first n can be added to J, time Complexity O (nKm), is not optimal.


Method 2: dp[i+1][j] = When the number of first I is added and the J is obtained, the number of the first I can be the maximum number of remaining. time Complexity O (NK).

For example: N=3, a={3,5,8}, m={3,2,2}, k=17.

I\j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Starting 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
0 (3,3) 3 -1 -1 2 -1 -1 1 -1 -1 0 -1 -1 -1 -1 -1 -1 -1 -1
1 (5,2) 2 -1 -1 2 -1 1 2 -1 1 2 0 -1 -1 0 1 -1 -1 -1
2 (8,2) 2 -1 -1 2 -1 2 2 -1 2 2 2 1 -1 1 1 -1 1 1

Code:

/* * main.cpp * * Created on:2014.7.20 * author:spike *//*eclipse CDT, gcc 4.8.1*/#include <stdio.h> #include <memory.h>class program {static const int max_n = 100;int N = 3;int K = 17;int A[max_n] = {3,5,8};int M[max_n] = {3 , 2,2};bool dp[max_n+1][max_n+1];p ublic:void Solve () {dp[0][0] = true;for (int i=0; i<n; ++i) {for (int j=0; j<=k; + +) j) {for (int k=0; k<=m[i]&&k*a[i]<=j; ++k) {DP [i+1][j] |= dp[i][j-k*a[i]];//or Operation}}}if (Dp[n][k]) printf ("Re Sult = yes\n "), Else printf (" result = no\n ");}}; Class Program2 {static const int max_n = 100;static const int max_k = 20;int N = 3;int K = 17;int A[max_n] = {3,5,8};int m [Max_n] = {3,2,2};int dp[max_k+1];p ublic:void Solve () {memset (DP,-1, sizeof (DP));DP [0] = 0;for (int i=0; i<n; ++i) {fo R (int j=0; j<=k; ++j) {if (Dp[j] >= 0) {Dp[j] = m[i];} else if (J < A[i] | | dp[j-a[i]]<=0) {DP[J] =-1;} else {Dp[j] = dp[j-a[i]]-1;}}} if (dp[k]>=0) printf ("result = yes\n"), Else printf ("result = no\n");}};int main (void) {Program2 ip;ip.solve (); return 0;} 

Output:

result = Yes










Programming algorithms-Multiple parts and problem codes (C)

Related Article

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.