【问题标题】:Recursive solution of ordered Coin Combinations II (CSES)有序硬币组合 II (CSES) 的递归解
【发布时间】:2021-11-30 23:56:07
【问题描述】:

Question Link

考虑一个由 n 个硬币组成的货币系统。每个硬币都有一个正整数值。你的任务是计算可以使用可用硬币产生货币总和 x 的不同有序方式的数量。

例如,如果硬币是 {2,3,5} 并且期望的总和是 9,则有 3 种方式:

2+2+5
3+3+3
2+2+2+3

我正在尝试为此编写递归解决方案。
我想出了this 代码。

#include<iostream>
#include<vector>
using namespace std;
#define M 1000000007
 
vector<int>arr;
vector<vector<int>>dp;

int solve(int i,int target){
    if(i>=(int)arr.size()) return 0;
    if(target<0) return 0;
    if(target==0) return 1;
    if(dp[i][target]!=-1) return dp[i][target];
    return dp[i][target]=(solve(i,target-arr[i])%M+solve(i+1,target)%M)%M;
}

int main(){
    int n,target;
    cin>>n>>target;
    arr.resize(n);
    dp.resize(n+1,vector<int>(target+1,-1));
    for(int i=0;i<n;++i){
        cin>>arr[i];
    }
    cout<<solve(0,target);
}

但此代码超出了时间限制。如何以递归方式编写此代码以使其被接受?这是non-recursive approach,但我想知道如何编写它的递归版本。

【问题讨论】:

标签: c++ algorithm dynamic-programming


【解决方案1】:

选择结果的递归求和示例如下:

void recursive_sum( IN const std::vector<int>& coins, 
                    IN int result,
                    OUT std::vector<std::string>& res,
                    IN std::deque<int> numbers = {},
                    IN int actual_sum = 0)
{
    for(auto& n: coins)
    {
        int nsum = actual_sum + n; 
        if(nsum == result)
            insert_result(res, copy_insert(numbers, n));
        else if(nsum < result)
            recursive_sum(coins, result, res, copy_insert(numbers, n), nsum);
    }
}

与此类似的 main 可以完成这项工作:

int main()
{
    std::vector<std::string> res{};
    std::vector<int> coins{};
    int sum_result = 0;
    
    input_coins(coins);
    input_sum_result(sum_result);
    recursive_sum(coins, sum_result, res);
    out_result(res);
    return 0;
}

如果您想查看此示例的完整版本,请单击here

输出示例:

"
Insert a number or 'next' to continue
10
Actually coins are: { 10, }
Insert a number or 'next' to continue 
15
Actually coins are: { 10, 15, }
Insert a number or 'next' to continue 
25
Actually coins are: { 10, 15, 25, }
Insert a number or 'next' to continue 
next
Insert the sum result:
100
Possible sums are: 
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, }
{10, 10, 10, 10, 10, 10, 10, 15, 15, }
{10, 10, 10, 10, 10, 10, 15, 15, 10, }
{10, 10, 10, 10, 10, 10, 15, 25, }
{10, 10, 10, 10, 10, 10, 25, 15, }
{10, 10, 10, 10, 10, 25, 10, 15, }
{10, 10, 10, 10, 10, 25, 25, }
{10, 10, 10, 10, 15, 15, 15, 15, }
{10, 10, 10, 15, 15, 10, 15, 15, }
{10, 10, 10, 15, 15, 15, 25, }
{10, 10, 10, 15, 25, 15, 15, }
{10, 10, 15, 15, 10, 25, 15, }
{10, 10, 15, 15, 15, 10, 10, 15, }
{10, 10, 15, 15, 15, 10, 25, }
{10, 10, 15, 15, 25, 25, }
{10, 15, 15, 15, 15, 15, 15, }
{10, 15, 15, 25, 10, 25, }
{10, 15, 25, 25, 10, 15, }
{10, 15, 25, 25, 25, }
{10, 25, 10, 10, 10, 10, 25, }
{10, 25, 10, 10, 15, 10, 10, 10, }
{15, 15, 15, 15, 15, 25, }
{15, 25, 15, 15, 15, 15, }
{25, 25, 25, 10, 15, }
{25, 25, 25, 15, 10, }
{25, 25, 25, 25, }
"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    相关资源
    最近更新 更多