博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hackerrank-knapsack
阅读量:7175 次
发布时间:2019-06-29

本文共 1994 字,大约阅读时间需要 6 分钟。

题目描述:

#include 
#include
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: 41 200012 105 92 20002 12 20001999 1999=======output:20001020001999=====input:31 656 83 3 3 3 3 39 109 4 4 9 4 9 9 9 9output:569*/#define MAXLEN 5000int 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
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
> 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
= 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;}

转载地址:http://zzbzm.baihongyu.com/

你可能感兴趣的文章
android: BaseAdapter和ListView简单运用(08)
查看>>
自带内存上的读写(openFileOutput和openFileInput)
查看>>
服务器搭建:3.2、openresty图片压缩之 lua调用GraphicsMagick
查看>>
bash 脚本编程 变量、变量类型 (笔记)
查看>>
win7 管理员权限
查看>>
docker下redis集群搭建
查看>>
composer出现proc_open,fileinfo问题
查看>>
无ROWID优化(The WITHOUT ROWID Optimization)
查看>>
Android第七课 探索Activity的生命周期
查看>>
求排列
查看>>
Cisco-CCNP之OSPF链路状态路由协议(三)
查看>>
CentOS 7 系列(一)系统服务 systemd
查看>>
Hive 数据倾斜总结
查看>>
mysql查询结果处理
查看>>
扫描识别控件Dynamic Web TWAIN v12.3.1发布,更新服务证书丨附下载
查看>>
VintaSoft PDF插件VintaSoftPDF.NET Plug-in更新至v5.6,新增多页查看模式
查看>>
windows环境中不重启电脑杀死占用某个端口的进程
查看>>
“90+68”的完美转变
查看>>
Kubernetes上的负载均衡详解
查看>>
centos7格式化大于2T的硬盘
查看>>