我正在构建一个<label id="lblpendingcount" class="labels">Levelwise Approval Pending Counts</label>
,它返回输入更改的硬币数。但是,我想修改代码,以便该函数还返回一个数组,其值为为示例返回的硬币:
recursive bottom-up dynamic programming function
我尝试打印Change:9c; #coins:3; 1c: 0, 2c:2, 5c:1
,但只从硬币元组中提取了一枚硬币。例如:
在values[j]
,可能的元组是iteration 7
,但我只得到每个元素的最后一个元素,即1,2,而我想要最后一个元组。
以下是此问题的C代码:
(5,1,1),(5,2)
答案 0 :(得分:1)
您没有随时可以获得的信息。
如果您为每次迭代存储最后一枚硬币,则可以打印硬币:
/**
* @desc returns the number of coins needed for a particular amount of change
* using bottom-up recursive dynamic programming approach.
* @param int amt - amount of change to convert to coins.
* int values[] - array of coins available (predefined).
* int n - length of values[].
* int a - current iteration of change (predefined at 1).
* int coins[] - array holding the amount of coins from 1 till amt.
* @return return coins[amt] - returns the amount of coins.
*/
int ComputeChange(int amt,int values[],int n,int a,int coins [], int lastcoin [])
{
printf("\n\na: %d",a);
printf("\n");
coins[a]=INT_MAX;
int j;
int tmp;
//loops all values of coins
for (j=0; j<n; j++)
{
// if current coin value is smaller or equal than a (the current change value)
// and if the number of coins at the current amount-the currently looped value of a coin
// is less than the number of coins at the current amount.
if ((values[j] <=a)&& (1+coins[a-values[j]]<coins[a]))
{
lastcoin[a] = values[j];
coins[a]=1+coins[a-values[j]];
}
}
if (a==amt)
{
j = amt;
while (j>0) {
printf("%d, ", lastcoin[j]); // Print the coins that make up the amount
j -= lastcoin[j];
}
printf("\n");
return coins[amt];
}
else
{
return ComputeChange(amt,values,n,a+1,coins);
}
}