Да, это возможно и довольно просто.
Вам просто нужно учитывать возвращаемый элемент: здесь int
, struct (int + history)
и функцию, которая объединяет ваше «возвращенное» значение: здесь сумма (1 + int)->int
для отслеживания изменения истории по
int -> 1 + int
// becomes
(int, history) -> (int+1, history + pieceTaken)
Рассмотрим структуру
struct NbCoin {
int nbCoin;
vector<int> history; // array of pieces you took during recursion
}
//now makeChangeRecursive returns the number of coin AND history
NbCoin makeChangeRecursive(int[] coins, int numCoins, int amount)
int r, l;
if (A == 0) return { nbCoin: 0, history: []}; //like before but with the empty history
else if (n == -1 || A < 0) return { nbCoin: -1, history: []}; // idem
// now contains our history as well
r = makeChangeRecursive(coins, numCoins - 1, amount);
// here you are taking some coin, so track it into history
l = makeChangeRecursive(coins, numCoins, amount - coins[numCoins]);
l = {
nbCoin: 1 + l.nbCoin, // like before
history : l.history.concat(coins[numCoins]) // pieceTaken is coins[numCoins]
// concat should create a __new__ array merging l.history and coins[numCoins]
}
// put nbCoin everywhere as our comparison key
if (r.nbCoin == -1 && l.nbCoin == 0) return { nbCoin: -1, []};
else if ((r.nbCoin == -1 || l.nbCoin < r.nbCoin) && l.nbCoin != 0) return l;
return r;
makeChangeRecursive({1, 2, 5}, 2, 11);
Везде, где вы управляли количеством монет, вы управляете struct.nbCoin
, и вы обновляете историю вместе с ним.
Я не проверял, в порядке ли ваш алгоритм, доверяя вам.
Код, который я изменил, теперь не java действителен, до вас!