C 从动态规划函数中获取值
我正在构造一个自底向上的递归动态规划函数,它返回输入的零钱的硬币数量。但是,我想修改代码,使函数还返回一个数组,其中包含返回的硬币值,例如:C 从动态规划函数中获取值,c,recursion,dynamic-programming,C,Recursion,Dynamic Programming,我正在构造一个自底向上的递归动态规划函数,它返回输入的零钱的硬币数量。但是,我想修改代码,使函数还返回一个数组,其中包含返回的硬币值,例如: Change:9c; #coins:3; 1c: 0, 2c:2, 5c:1 我尝试打印值[j],但从硬币元组中只提取了一枚硬币。例如: 在迭代7中,可能的元组是(5,1,1)、(5,2),但我只得到每个元素的最后一个元素,即1,2,而我想要的是最后一个元组 以下是此问题的C代码: /** * @desc returns the number
Change:9c; #coins:3; 1c: 0, 2c:2, 5c:1
我尝试打印值[j]
,但从硬币元组中只提取了一枚硬币。例如:
在迭代7
中,可能的元组是(5,1,1)、(5,2)
,但我只得到每个元素的最后一个元素,即1,2,而我想要的是最后一个元组
以下是此问题的C代码:
/**
* @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 [])
{
printf("\n\na: %d",a);
printf("\n");
coins[a]=INT_MAX;
int j;
//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]))
{
printf("%d,",values[j]);
coins[a]=1+coins[a-values[j]];
}
}
if (a==amt)
{
printf("\n");
return coins[amt];
}
else
{
return ComputeChange(amt,values,n,a+1,coins);
}
}
int main()
{
int i;
int counter=0;
int answer;
int choice=0;
int array[] = {1,2,5,10,20,50,100,200};
int answers[100][2];
int n= sizeof(array)/sizeof(array[0]);
while (1)
{
printf("Enter change to turn into coins, -1 to exit:\n");
scanf("%d",&choice);
if (choice==-1)
{
exit(0);
}
int isFound=0;
int coins[choice]; //array of number of coins needed up to amt
coins[0]=0;
for (i=0; i<counter; i++)
{
if (choice==answers[i][0])
{
printf("Cached Value: ");
answer=answers[i][1];
isFound=1;
}
}
if (!isFound)
{
answer=ComputeChange(choice,array,n,1,coins);
}
answers[counter][0]=choice;
answers[counter++][1]=answer;
printf("Number of coins: %d\n\n",answer);
}
return 0;
}
/**
*@desc返回特定金额的零钱所需的硬币数量
*采用自下而上的递归动态规划方法。
*@param int amt-转换为硬币的变化量。
*int values[]-可用硬币数组(预定义)。
*int n—值的长度[]。
*int a—更改的当前迭代(在1处预定义)。
*int coins[]-从1到amt持有硬币数量的数组。
*@return coins[amt]-返回硬币数量。
*/
整数计算范围(整数金额、整数值[]、整数n、整数a、整数硬币[])
{
printf(“\n\na:%d”,a);
printf(“\n”);
硬币[a]=整数最大值;
int j;
//循环所有硬币的值
对于(j=0;j你没有现成的信息
如果为每次迭代存储最后一枚硬币,则可以打印硬币:
/**
* @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);
}
}
/**
*@desc返回特定金额的零钱所需的硬币数量
*采用自下而上的递归动态规划方法。
*@param int amt-转换为硬币的变化量。
*int values[]-可用硬币数组(预定义)。
*int n—值的长度[]。
*int a—更改的当前迭代(在1处预定义)。
*int coins[]-从1到amt持有硬币数量的数组。
*@return coins[amt]-返回硬币数量。
*/
整数计算范围(整数金额、整数值[]、整数n、整数a、整数硬币[]、整数最后硬币[])
{
printf(“\n\na:%d”,a);
printf(“\n”);
硬币[a]=整数最大值;
int j;
int tmp;
//循环所有硬币的值
对于(j=0;j此函数如何调用?请给出一个示例coins[a]=(int)无穷大看起来有点可疑。浮点值有无穷大的概念,而积分没有。如果目的是使用尽可能大的值,请使用limits.h中的INT_MAX
,然后放弃转换。我不理解a
的用法。是当前的迭代还是当前的更改量?这似乎不同ngs位于代码的不同位置。a
是当前迭代,用于计算硬币的最大金额。a
从1开始,每次迭代递增,直到amt
谢谢!因此为了缓存最后一枚硬币,我可以返回最后一枚硬币并移动if(a==amt)的块
在主函数中。我是否应该使用指针访问硬币[amt],因为lastcoin现在使用返回值?