我有一个递归算法,可以按照以下方式进行更改:
public static int makeChange(int amount, int currentCoin) {
//if amount = zero, we are at the bottom of a successful recursion
if (amount == 0){
//return 1 to add this successful solution
return 1;
//check to see if we went too far
}else if(amount < 0){
//don't count this try if we went too far
return 0;
//if we have exhausted our list of coin values
}else if(currentCoin < 0){
return 0;
}else{
int firstWay = makeChange(amount, currentCoin-1);
int secondWay = makeChange(amount - availableCoins[currentCoin], currentCoin);
return firstWay + secondWay;
}
}
然而,我希望增加能够存储或打印每个成功返回的组合的功能。但是,我有点难以理解如何实现这一点。原始算法很简单,但现在我感到沮丧。有什么建议吗?
CB