zoukankan      html  css  js  c++  java
  • hackerrank-knapsack

    https://www.hackerrank.com/challenges/unbounded-knapsack 题目描述:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    /*
      desc:complete knapsack problem, each items can select zero or more times.
      auther: justinzhang(aktiger87@gmail.com)
      time:2015年2月15日10:14:02
      testcase#7:
      input:
      4
    1 2000
    1
    2 10
    5 9
    2 2000
    2 1
    2 2000
    1999 1999
    =======
    output:
    2000
    10
    2000
    1999
    
    =====
    input:
    3
    1 6
    5
    6 8
    3 3 3 3 3 3
    9 10
    9 4 4 9 4 9 9 9 9
    
    output:
    5
    6
    9
    
    */
    
    #define MAXLEN  5000
    
    
    int main() {
    
      int T = 0;   // The number of test cases;
      
      int n = 0, k = 0;  // n is the number of integers, k is the volume of packages.
      
      int **array =new int* [MAXLEN];
      for(int i = 0; i< MAXLEN; i++) {
        array[i] = new int[MAXLEN];
      }
      
    
    
      int tmp = 0;
    
      cin>>T;
      
      for(int i = 0; i < T; i++) {
        vector<int> arrayN;  // arrayN should be in this for loop, if it is a global one, you should clear it each time.
    
        cin >> n; // the number of integers.
        cin >> k; // the volume of package.
        for(int j=0; j<n; j++) {
    
          cin >> tmp;
          arrayN.push_back(tmp);
    
        }
    
        
        // here we need to construct the logic of select zero or more times for each item.
        for(int j = 0; j < n; j++) {
          int maxSelectNum = k/arrayN[j];
    
          for(int select = 0; select < maxSelectNum; select++) {
    
        int item = arrayN[j];
        arrayN.push_back(item);
        
          }
          
        } // end of int j=0  , here we finish transition of complete knapsack problem to zeroone knapsack problem
        
        //    cout << "size of arrayN is :" << arrayN.size() << endl;
    
        for(int i=0; i<arrayN.size(); i++) {
          //                  cout << "arrayN:" << arrayN[i] << endl;
        }
        //cout << "end output arrayN" << endl;
        
        for(int vecIndex = 0; vecIndex <=arrayN.size(); vecIndex++) {
          for(int volIndex = 0; volIndex <=k ; volIndex++) {
    
        array[vecIndex][volIndex] = 0;
    
          }
        }
    
    
        
        
        for(int vecIndex = 1; vecIndex <= arrayN.size(); vecIndex++) {
    
          for(int volIndex = 1; volIndex <= k; volIndex++) {
        int v1 = 0;
        int v2 = 0;
    
        v1 = array[vecIndex-1][volIndex];
    
        if(volIndex - arrayN[vecIndex] >= 0 ) {  // here we use >=0 , not >0, >0 will be an error, =0 filled the package.
          
          v2 = array[vecIndex-1][volIndex-arrayN[vecIndex]] + arrayN[vecIndex];
    
        }
    
        if(v1 > v2 ) {
    
          array[vecIndex][volIndex] = v1;
    
        } else {
    
          array[vecIndex][volIndex] = v2;
    
        }
    
          } // end of for loop volIndex 
        } // end of for loop int vecIndex
        
        cout << array[arrayN.size()][k] << endl;
      } // end of loop for int T
    
      //释放动态申请的内存
      for(int i =0; i < MAXLEN; i++) {
    
        delete [] array[i];
      }
    
      delete[]array;
    
      return 0;
    }

    image

  • 相关阅读:
    Asp.NET 4.0 ajax实例DataView 模板编程1
    ASP.NET 4.0 Ajax 实例DataView模板编程 DEMO 下载
    部分东北话、北京话
    .NET 培训课程解析(一)
    ASP.NET 4.0 Ajax 实例DataView模板编程2
    ASP.NET Web Game 架构设计1服务器基本结构
    ASP.NET Web Game 构架设计2数据库设计
    TFS2008 基本安装
    Linux上Oracle 11g安装步骤图解
    plsql developer远程连接oracle数据库
  • 原文地址:https://www.cnblogs.com/justinzhang/p/4292837.html
Copyright © 2011-2022 走看看