Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 迭代方法似乎比递归实现慢(换硬币)_C++_Algorithm_Recursion_Dynamic Programming_Coin Change - Fatal编程技术网

C++ 迭代方法似乎比递归实现慢(换硬币)

C++ 迭代方法似乎比递归实现慢(换硬币),c++,algorithm,recursion,dynamic-programming,coin-change,C++,Algorithm,Recursion,Dynamic Programming,Coin Change,#包括 使用名称空间std; #定义garbaze 0 //可以进行更改的方法的数量 国际货币[]={garbaze,50,25,10,5,1}//顺序并不重要//就像//计数方式一样。。。我们正在返回的函数 //0如果现在哪个是的差异是递归解记住以前任务中的部分解(因为DP表是全局的,不会在不同的输入之间删除),而迭代不会-对于每个新输入,它从头开始重新计算DP矩阵 可以通过记住DP表的哪一部分已经计算过并避免重新计算,而不是对每个查询从头开始重新计算来解决此问题。这可能更好地位于上。我们没有

#包括
使用名称空间std;
#定义garbaze 0
//可以进行更改的方法的数量
国际货币[]={garbaze,50,25,10,5,1}//顺序并不重要//就像//计数方式一样。。。我们正在返回的函数

//0如果现在哪个是的差异是递归解记住以前任务中的部分解(因为DP表是全局的,不会在不同的输入之间删除),而迭代不会-对于每个新输入,它从头开始重新计算DP矩阵


可以通过记住DP表的哪一部分已经计算过并避免重新计算,而不是对每个查询从头开始重新计算来解决此问题。

这可能更好地位于上。我们没有关于UVA online judge引擎内部工作的任何信息。可能你应该首先测量任何差异。另外,它可能是编译时间也被认为是由UVA。但你至少可以告诉我,哪种方法应该更快。。。我只是不明白为什么第二种方法需要3秒以上,而第一种方法需要0.024秒。似乎我忘了在第二种方法中存储预先计算的值……所以每次都是从头开始做
#include <cstdio>


using namespace std;

#define garbaze 0
//number of ways changes can be made
int coins[] = {garbaze,50,25,10,5,1}; //order does not matter//as         in         the     //count_ways... function we are returning
//0 if which_coin_now is <= 0 so it
//does n't matter what we have in the index 0 [garbaze] .. but we must put //something there to implement the
//code using the pseudo code or recursive relation
typedef unsigned long long ull; //simple typedef
ull dp[7490][6]; //2d table
//recursive approach


ull count_ways_of_changes(int money_now,int which_coin_now)
{
    if(money_now == 0)
        return 1;
    if(money_now < 0 || which_coin_now <=0 )
        return 0;
    if(dp[money_now][which_coin_now] == -1)
        dp[money_now][which_coin_now] = count_ways_of_changes(money_now,which_coin_now-1) //excluding current coin
        + count_ways_of_changes(money_now - coins[which_coin_now],which_coin_now) ; //including current coin


    return dp[money_now][which_coin_now] ;


}

int main()
{
    for(int loop = 0; loop< 7490 ;loop++)
        for(int sec_loop = 0;sec_loop<6;sec_loop++)
                 dp[loop][sec_loop] = -1; //table initialization
int N = 0;
while(scanf("%d",&N)==1)
{
    printf("%llu\n",count_ways_of_changes(N,5)); //llu for unsigned long long
}
return 0;
}
#include <cstdio>
//#include <iostream>
//using namespace std;
typedef unsigned long long ull;
ull dp[7490][6];
#define garbaze 0
int value_coins[] = {garbaze,5,1,10,25,50} ;
inline ull count_ways_change(int money,int num_of_coins)
{
    for(int sum_money_now = 0; sum_money_now <= money ;sum_money_now++)
        for(int recent_coin_index = 0 ; recent_coin_index <= num_of_coins ; recent_coin_index++)
//common mistakes : starting the second index at num_of_coins and decrementing till 0 ...see we are pre calculating
//we have to start bottom to up....if we start at dp[0][5] .....to dp[1][5] but to know that i need to know
//dp[1][4] and dp[..][5] before hand ..but we have not calculated dp[1][4] yet...in this case i don't go to infinite
//loop or anything as the loop is well defined but i get stupid garbaze answer

    {
        if(sum_money_now == 0)
            dp[sum_money_now][recent_coin_index] = 1;
        else if(recent_coin_index == 0)
            dp[sum_money_now][recent_coin_index] = 0;
        else if(sum_money_now < value_coins[recent_coin_index] && recent_coin_index != 0)
            dp[sum_money_now][recent_coin_index] = dp[sum_money_now][recent_coin_index-1] ;
            else
                dp[sum_money_now][recent_coin_index] = dp[sum_money_now][recent_coin_index-1] + dp[sum_money_now - value_coins[recent_coin_index] ][recent_coin_index] ;
 //   cout<<dp[sum_money_now][recent_coin_index]<<endl;
    }


    return dp[money][num_of_coins] ;
}
int main()
{/*
    for(int loop = 0; loop< 7490 ;loop++)
        for(int sec_loop = 0;sec_loop<6;sec_loop++)
                 dp[loop][sec_loop] = -1; //table initialization

*/ //In the iterative version do not need to initialize the table as we are working bottom - up
int N = 0;
while(scanf("%d",&N)==1)
{

printf("%llu\n",count_ways_change(N,5)); //llu for unsigned long long

}
return 0;
}