From e5b43e76320c0f21b8b6e6527d463a4c517aa9ab Mon Sep 17 00:00:00 2001 From: Shoaib Rayeen Date: Wed, 11 Sep 2019 22:01:47 +0530 Subject: [PATCH 1/2] Change Iterative Function - Merge Print and Iterative function --- Problems/KnapsackProblem/pack.cpp | 87 ++++++++++++++----------------- 1 file changed, 39 insertions(+), 48 deletions(-) diff --git a/Problems/KnapsackProblem/pack.cpp b/Problems/KnapsackProblem/pack.cpp index 7404516..fb751ec 100644 --- a/Problems/KnapsackProblem/pack.cpp +++ b/Problems/KnapsackProblem/pack.cpp @@ -2,60 +2,33 @@ #include using namespace std; -int *w = NULL; // 存储每件物品重量的数组指针 -int *v = NULL; // 存储每件物品价值的数组指针 -int **T = NULL; // 存储背包问题表格的数组指针 -int n; // 物品个数n -int W; // 背包总承重W - // 返回两个值的最大值 -int max(int a, int b) -{ +int max(int a, int b) { return (a > b) ? a : b; } // 迭代法,能显示背包问题的表格 -void packIterative() -{ +int packIterative(int n, int W, int *w, int *v) { // 循环遍历n行 - for (int i = 1; i <= n; ++i) - { + int T[i+1][j+1]; + for (int i = 0; i <= n; ++i) { // 循环遍历W列 - for (int j = 1; j <= W; ++j) - { + for (int j = 0; j <= W; ++j) { + if (i == 0 || j == 0) { + T[i][j] = 0; + } //第i个物品能装下,则比较包括第i个物品和不包括第i个物品,取其最大值 - if (w[i] <= j) - T[i][j] = max(v[i] + T[i - 1][j - w[i]], T[i - 1][j]); - + if ( w[i-1] <= j) { + T[i][j] = max(v[i-1] + T[i - 1][j - w[i]], T[i - 1][j]); + } // 第i个物品不能装下,则递归装i-1个 - else + else { T[i][j] = T[i - 1][j]; + } } } -} - -// 递归法,不支持显示背包问题的表格 -int packRecursive(int i, int j, int *w, int *v) -{ - // 结束条件(初始条件),i或者j为0时最大总价值为0 - if (i == 0 || j == 0) - return 0; - - // 第i个物品不能装下,则递归装i-1个 - if (w[i] > j) - return packRecursive(i - 1, j, w, v); - - //第i个物品能装下,则比较包括第i个物品和不包括第i个物品,取其最大值 - else - return max(v[i] + packRecursive(i - 1, j - w[i], w, v), packRecursive(i - 1, j, w, v)); -} - -// 打印背包问题的表格 -void printT(int n, int W) -{ - // 打印n行 - for (auto i = 0; i <= n; i++) - { + + for (auto i = 0; i <= n; i++) { // 打印行数 cout << i << ":\t"; @@ -68,10 +41,31 @@ void printT(int n, int W) // 换行 cout << endl; } + return T[n][W]; } -int main() -{ +// 递归法,不支持显示背包问题的表格 +int packRecursive(int n, int W, int *w, int *v) { + // 结束条件(初始条件),i或者j为0时最大总价值为0 + if (n == 0 || W == 0) { + return 0; + } + // 第i个物品不能装下,则递归装i-1个 + if (w[i] > W) { + return packRecursive(n - 1, W, w, v); + } + //第i个物品能装下,则比较包括第i个物品和不包括第i个物品,取其最大值 + else { + return max(v[i] + packRecursive(n - 1, W - w[n], w , v), packRecursive(n - 1, W, w, v)); + } +} + + +int main() { + int *w = NULL; // 存储每件物品重量的数组指针 + int *v = NULL; // 存储每件物品价值的数组指针 + int n; // 物品个数n + int W; // 背包总承重W cout << "---------------- 背包问题 ----------------" << endl; cout << "请输入物品数 n (n>=0) " << endl; @@ -139,10 +133,7 @@ int main() case 1: { // 迭代法,能显示背包问题的表格 - packIterative(); - cout << "能装下物品的最大价值为 " << T[n][W] << endl; - cout << "------------------------------------------------" << endl; - printT(n, W); + cout << "能装下物品的最大价值为 " << packIterative(n, W, w, v) << endl; break; } case 2: From e7782962b7abb9a4be0785dc9217dac9ed2a9cd0 Mon Sep 17 00:00:00 2001 From: huihut Date: Thu, 12 Sep 2019 02:09:58 +0800 Subject: [PATCH 2/2] Update pack.cpp Your PR can't be compiled, I modified some code. 1. Added the definition of T 2. Added delete heap variables 3. packIterative and printT separate two functions to uncouple code --- Problems/KnapsackProblem/pack.cpp | 82 ++++++++++++++++++------------- 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/Problems/KnapsackProblem/pack.cpp b/Problems/KnapsackProblem/pack.cpp index fb751ec..60f0d8b 100644 --- a/Problems/KnapsackProblem/pack.cpp +++ b/Problems/KnapsackProblem/pack.cpp @@ -2,6 +2,8 @@ #include using namespace std; +int **T = NULL; // 存储背包问题表格的数组指针 + // 返回两个值的最大值 int max(int a, int b) { return (a > b) ? a : b; @@ -9,26 +11,47 @@ int max(int a, int b) { // 迭代法,能显示背包问题的表格 int packIterative(int n, int W, int *w, int *v) { + // 循环遍历n行 - int T[i+1][j+1]; - for (int i = 0; i <= n; ++i) { + for (int i = 1; i <= n; ++i) + { // 循环遍历W列 - for (int j = 0; j <= W; ++j) { - if (i == 0 || j == 0) { - T[i][j] = 0; - } + for (int j = 1; j <= W; ++j) + { //第i个物品能装下,则比较包括第i个物品和不包括第i个物品,取其最大值 - if ( w[i-1] <= j) { - T[i][j] = max(v[i-1] + T[i - 1][j - w[i]], T[i - 1][j]); - } + if (w[i] <= j) + T[i][j] = max(v[i] + T[i - 1][j - w[i]], T[i - 1][j]); + // 第i个物品不能装下,则递归装i-1个 - else { + else T[i][j] = T[i - 1][j]; - } } } - - for (auto i = 0; i <= n; i++) { + return T[n][W]; +} + +// 递归法,不支持显示背包问题的表格 +int packRecursive(int n, int W, int *w, int *v) { + // 结束条件(初始条件),i或者j为0时最大总价值为0 + if (n == 0 || W == 0) { + return 0; + } + // 第i个物品不能装下,则递归装i-1个 + if (w[n] > W) { + return packRecursive(n - 1, W, w, v); + } + //第i个物品能装下,则比较包括第i个物品和不包括第i个物品,取其最大值 + else { + return max(v[n] + packRecursive(n - 1, W - w[n], w, v), packRecursive(n - 1, W, w, v)); + } +} + +// 打印背包问题的表格 +void printT(int n, int W) +{ + // 打印n行 + for (auto i = 0; i <= n; i++) + { // 打印行数 cout << i << ":\t"; @@ -41,26 +64,8 @@ int packIterative(int n, int W, int *w, int *v) { // 换行 cout << endl; } - return T[n][W]; } -// 递归法,不支持显示背包问题的表格 -int packRecursive(int n, int W, int *w, int *v) { - // 结束条件(初始条件),i或者j为0时最大总价值为0 - if (n == 0 || W == 0) { - return 0; - } - // 第i个物品不能装下,则递归装i-1个 - if (w[i] > W) { - return packRecursive(n - 1, W, w, v); - } - //第i个物品能装下,则比较包括第i个物品和不包括第i个物品,取其最大值 - else { - return max(v[i] + packRecursive(n - 1, W - w[n], w , v), packRecursive(n - 1, W, w, v)); - } -} - - int main() { int *w = NULL; // 存储每件物品重量的数组指针 int *v = NULL; // 存储每件物品价值的数组指针 @@ -94,8 +99,8 @@ int main() { // 分配空间 // 对w和v分配n+1大小 - w = new int[n+1]; - v = new int[n+1]; + w = new int[n + 1]; + v = new int[n + 1]; // 对T分配n+1行,并初始化为0 T = new int *[n + 1](); @@ -108,7 +113,7 @@ int main() { // 输入背包的重量和价值 for (auto i = 1; i <= n; i++) { - cout << "请输入第 " << i << " 个背包的重量和价值(用空格隔开)" << endl; + cout << "请输入第 " << i << " 个物品的重量和价值(用空格隔开)" << endl; cin >> w[i] >> v[i]; if (cin.fail() || w[i] < 0 || v[i] < 0) { @@ -134,6 +139,8 @@ int main() { { // 迭代法,能显示背包问题的表格 cout << "能装下物品的最大价值为 " << packIterative(n, W, w, v) << endl; + cout << "------------------------------------------------" << endl; + printT(n, W); break; } case 2: @@ -151,6 +158,13 @@ int main() { cout << "------------------------------------------------" << endl; + delete w; + delete v; + for (int i = 0; i <= n; ++i) { + delete[] T[i]; + } + delete[] T; + system("pause"); return 0; }