算法---动态规划0/1背包与找零钱
------------恢复内容开始------------
关于动态规划法,我怎么觉得就是蛮力法,有时候比蛮力法看起来还复杂
1.0/1背包问题
目前存在的问题,可能可以忽视?
#include<iostream>
#include<algorithm>
using namespace std;
int KnapSack(int w[], int v[], int n, int c);
int main()
{
int w[6] = { 0,2,2,6,5,4 };
int v[6] = { 0,6,3,5,4,6 };
int n = 5, c = 10;
cout << KnapSack(w, v, n, c) << endl;
}
#include<algorithm>
using namespace std;
int KnapSack(int w[], int v[], int n, int c);
int main()
{
int w[6] = { 0,2,2,6,5,4 };
int v[6] = { 0,6,3,5,4,6 };
int n = 5, c = 10;
cout << KnapSack(w, v, n, c) << endl;
}
int KnapSack(int w[], int v[], int n, int c) {
int i, j;int V[6][11] = { {0} };
int x[6];
for (i = 0;i <= n;i++) {
V[i][0] = 0;
}
for (j = 0;j <= c;j++) {
V[0][j] = 0;
}
for (i = 1;i <= n;i++) {
for (j = 1;j <= c;j++) {
if (j < w[i]) {
V[i][j] = V[i - 1][j];
}
else {
V[i][j] = max(V[i - 1][j], V[i - 1][j - w[i]] + v[i]);
}
}
}
for (j = c, i = n;i > 0;i--) {
if (V[i][j] > V[i-1][j]) {
x[i] = 1;j = j - w[i];
}
else x[i] = 0;
}
int i, j;int V[6][11] = { {0} };
int x[6];
for (i = 0;i <= n;i++) {
V[i][0] = 0;
}
for (j = 0;j <= c;j++) {
V[0][j] = 0;
}
for (i = 1;i <= n;i++) {
for (j = 1;j <= c;j++) {
if (j < w[i]) {
V[i][j] = V[i - 1][j];
}
else {
V[i][j] = max(V[i - 1][j], V[i - 1][j - w[i]] + v[i]);
}
}
}
for (j = c, i = n;i > 0;i--) {
if (V[i][j] > V[i-1][j]) {
x[i] = 1;j = j - w[i];
}
else x[i] = 0;
}
return V[n][c];
}
}
------------恢复内容结束------------
找零钱问题
建立了一个三维数组存取每个硬币的数量
但是在 int V[6][99] = { {0} }, L[6][99][6] = { { { 0 } } };
不定义V,L的输出就会变很奇怪,希望取得答案。
#include<iostream> using namespace std; int* find(int change, int Change[], int n); int main() { int i, Collection, Price; cin >> Collection >> Price; int change = Collection - Price; int Change[6] = { 0,1,2,5,7,10 }; int count = 0, n = 5; int* L;L = find(change, Change, n); cout << "应找金额:" << endl; for (i = 1;i <= 5;i++) cout << Change[i] << "元硬币的个数:" << L[i] << endl; system("pause"); } int* find(int change, int Change[], int n) { int i, j; int V[6][99] = { {0} }, L[6][99][6] = { { { 0 } } }; for (i = 1;i <= n;i++) { for (j = 1;j <= change;j++) { L[i][j][i] = j / Change[i];int m = i; int x;int dchange = j; for (x = i - 1;x > 0;x--) { int k = Change[x + 1]* L[i][dchange][x + 1]; dchange -= k;m -= 1; // if (m == 0)L[i][j][x] = change; L[i][j][x] = L[m][dchange][x]; } } }return L[n][change]; }
2019年11月21日
错误原因:
如果不设置足够大的V,会释放函数内申请的内存;
所以由函数传入一个数组,然后存储再传出
1 #include<iostream> 2 using namespace std; 3 void find(int change, int Change[], int n, int res[]); 4 int main() { 5 int i, Collection, Price; 6 cin >> Collection >> Price; 7 int change = Collection - Price; 8 int Change[6] = { 0,1,2,5,7,10 }; 9 int count = 0, n = 5; 10 int L[6]; 11 find(change, Change, n, L); 12 cout << "应找金额:" << endl; 13 for (i = 1; i <= 5; i++) 14 cout << Change[i] << "元硬币的个数:" << L[i] << endl; 15 16 } 17 void find(int change, int Change[], int n, int res[]) { 18 int i, j; 19 int V[6][99] = { {0} }, L[6][99][6] = { { { 0 } } }; 20 21 22 for (i = 1; i <= n; i++) { 23 for (j = 1; j <= change; j++) { 24 L[i][j][i] = j / Change[i]; int m = i; 25 int x; int dchange = j; 26 for (x = i - 1; x > 0; x--) { 27 int k = Change[x + 1] * L[i][dchange][x + 1]; 28 29 dchange -= k; m -= 1; 30 // if (m == 0)L[i][j][x] = change; 31 L[i][j][x] = L[m][dchange][x]; 32 } 33 34 } 35 } 36 for (int i = 1; i <= n; i++) { 37 res[i] = L[n][change][i]; 38 } 39 }