有数组penny,penny中所有的值都为正数且不重复。每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim(小于等于1000)代表要找的钱数,求换钱有多少种方法。
给定数组penny及它的大小(小于等于50),同时给定一个整数aim,请返回有多少种方法可以凑成aim。
测试样例:
[1,2,4],3,3
返回:2
class Exchange {
public:
int countWays(vector<int> penny, int n, int aim) {
// write code here
vector<vector<int> > temp(n,vector<int>(aim+1));
for(int i=0; i<n; i++){
temp[i][0] = 1;
}
for(int j=0; j<=aim; j++){
if((j%penny[0]) == 0)
temp[0][j] = 1;
else
temp[0][j] = 0;
}
for(int i=1; i<n; i++){
for(int j=1; j<=aim; j++){
if(j>=penny[i])
temp[i][j] = temp[i-1][j] + temp[i][j-penny[i]];
else
temp[i][j] = temp[i-1][j];
}
}
return temp[n-1][aim];
}
};