Firstly you should replace:
return count(n - coins[0]) + count(n - coins[1]) + count(n - coins[2]);
With a loop:
int nCoins = 0;
for(int coin=0; coin<coins.length; coin++)
{
nCoins += count(n-coins[coin]);
}
return nCoins;
The trouble with this is that it'll generate all permutations of coins (=634). To get each unique combination of coins you need to make sure you start each level of recursion at the current coin. For this you need to add an argument to your count method, to indicate the position in the coin array at which to start.
public static int count(int n, int startCoin)
Your loop then becomes
int nCoins = 0;
for(int coin=startCoin; coin<coins.length; coin++)
{
nCoins += count(n-coins[coin], coin);
}
return nCoins;
Which gives the correct answer (14).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…