我正在尝试使用动态编程方法解决硬币找零问题。我所做的是使用了占用空间较小的一个。这是我的代码:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int coinchange(int S[],int m,int n){
long long table[n+1];
// Initialize all table values as 0
memset(table, 0, sizeof(table));
// Base case (If given value is 0)
table[0] = 1;
// Pick all coins one by one and update the table[] values
// after the index greater than or equal to the value of the
// picked coin
for(int i=0; i<m; i++)
for(int j=S[i]; j<=n; j++)
table[j] += table[j-S[i]];
return table[n];
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int nn,mm,ar[250];
cin>>mm>>nn;
for(int i=0;i<nn;i++)
cin>>ar[i];
long long c=coinchange(ar,nn,mm);
cout<<c;
return 0;
}
它显示以下测试用例的错误答案:输入:250 24 41 34 46 9 37 32 42 21 7 13 1 24 3 43 2 23 8 45 19 30 29 18 35 11 预期输出:15685693751