我想在C++中建立遞歸調用硬幣更改。我在互聯網上嘗試了大部分算法,但它似乎不適用於矢量或者它沒有使用硬幣的總和。任何人都可以幫助我理解遞歸函數必須調用什麼?所以我的算法沒有給我使用的最小數量的硬幣,我不知道如何保存使用的硬幣。如何在C++中編程遞歸硬幣更改?
int coin(vector<int> denom, int s,int N)
{
if(N == 0)
{
return 1;
}
if(N < 0 || (N > 0 && s < 0))
{
return 0;
}
return min(coin(denom,s - 1, N), 1 + coin(denom, s,N-denom[s-1]));
}
Input a value N:
Input: 40
Input how many denominations:
Input: 3
Denominations #1:
Input: 5
Denominations #2:
Input: 20
Denominations #3:
Input: 30
Output:
Minimum # of coins: 2
Coin used: 20 + 20
Don't want: 30 + 5 + 5
什麼是's'和'N'在這方面?你能否明確地定義問題,關於你想達到的目標? –
s是大小,N是數字。我想要這個函數給我最小的#號硬幣並且產生使用的硬幣的結果 – Darkflame
我在這裏沒有看到任何數組。 – xaxxon