"Fun learning algorithm" dynamic programming

2019-11-30

10:05:54

 

 

#include <bits / STDC ++ H.> 
#include <stdlib.h> 
#include <stdio.h>
 the using  namespace STD;
 #define MAXN 10005
 #define M 105
 int C [M] [MAXN];
 int W [M], V [M];
 int X [M]; // X [M] represents whether the i-th item Add to cart 
int main () {
     int i, J, n-, W is; 
    COUT << " Please enter one article number: " ; 
    cin >> the n-; 
    cout << " Please enter the shopping cart capacity: " ; 
    cin >> W; 
    cout<< " Please enter the weight value w and v each item, separated by a space: " ;
     for ( int I = . 1 ; I <= n-; ++ I) { 
        CIN >> w [I] >> v [I ]; 
    } 
    for (I = 0 ; I <= n-; ++ I) { 
        C [I] [ 0 ] = 0 ; 
    } 
    for (J = 0 ; J <= W is; ++ J) { 
        C [ 0 ] [J] = 0 ; 
    } 
    for (I = . 1 ; I <= n-; ++ I) {
         for ( int J = . 1 ; J <= W is; ++J) {
             IF (J <W [I]) { // when the article is greater than the weight capacity of the cart, not into the 
                C [I] [J] = C [I- . 1 ] [J]; 
            } 
            the else { 
                C [ I] [J] = max (C [I- . 1 ] [J], C [I- . 1 ] [JW [I]] + ; V [I]) 
            } 
        } 
    } 
    COUT << " maximum load of cart value: " << C [n-] [W is] << endl;
     // reverse optimum configured 
    J = W is;
     for (I = n-; I> 0 ; - I) {
         IF (C [I] [ J]> C [I- . 1  ] [J]) {
            X [I]= . 1 ; 
            J - = W [I]; 
        } the else { 
            X [I] = 0 ; 
        } 
    } 
    COUT << " loaded shopping cart article is: " << endl;
     for ( int I = . 1 ; I <= n-; ++ I) {
         IF (X [I] == . 1 ) { 
            COUT << I << "  " ; 
        } 
    } 
    System ( " PAUSE " );
     return  0 ;
     
} 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/JasonPeng1/p/11961516.html