Algorithm to determine coin combinations

后端 未结 13 2541
臣服心动
臣服心动 2020-12-25 08:33

I was recently faced with a prompt for a programming algorithm that I had no idea what to do for. I\'ve never really written an algorithm before, so I\'m kind of a newb at t

相关标签:
13条回答
  • 2020-12-25 08:43

    You basically have to solve the following equation: 50 = a*4 + b*6 + c*10 + d*15, where the unknowns are a,b,c,d. You can compute for instance d = (50 - a*4 - b*6 - c*10)/15 and so on for each variable. Then, you start giving d all the possible values (you should start with the one that has the least possible values, here d): 0,1,2,3,4 and than start giving c all the possible values depending on the current value of d and so on.

    0 讨论(0)
  • 2020-12-25 08:48

    Here's a recursive solution in Java:

    // Usage: int[] denoms = new int[] { 1, 2, 5, 10, 20, 50, 100, 200 };       
    // System.out.println(ways(denoms, denoms.length, 200));
    public static int ways(int denoms[], int index, int capacity) {
        if (capacity == 0) return 1;
        if (capacity < 0 || index <= 0 ) return 0;
        int withoutItem = ways(denoms, index - 1, capacity); 
        int withItem = ways(denoms, index, capacity - denoms[index - 1]); 
        return withoutItem + withItem;
    }
    
    0 讨论(0)
  • 2020-12-25 08:50

    An algorithm is a procedure for solving a problem, it doesn't have to be in any particular language.

    First work out the inputs:

    typedef int CoinValue;
    
    set<CoinValue> coinTypes;
    int value;
    

    and the outputs:

    set< map<CoinValue, int> > results;
    

    Solve for the simplest case you can think of first:

    coinTypes = { 1 }; // only one type of coin worth 1 cent
    value = 51;
    

    the result should be:

    results = { [1 : 51] }; // only one solution, 51 - 1 cent coins
    

    How would you solve the above?

    How about this:

    coinTypes = { 2 };
    value = 51;
    
    results = { }; // there is no solution
    

    what about this?

    coinTypes = { 1, 2 };
    value = { 4 };
    
    results = { [2: 2], [2: 1, 1: 2], [1: 4] }; // the order I put the solutions in is a hint to how to do the algorithm.
    
    0 讨论(0)
  • 2020-12-25 08:56

    This problem is well known as coin change problem. Please check this and this for details. Also if you Google "coin change" or "dynamic programming coin change" then you will get many other useful resources.

    0 讨论(0)
  • 2020-12-25 08:59

    For such a small number of coins you can write a simple brute force solution.

    Something like this:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    vector<int> v;
    
    int solve(int total, int * coins, int lastI)
    {
        if (total == 50) 
        {
            for (int i = 0; i < v.size(); i++)
            {
                cout << v.at(i) << ' ';
            }
            cout << "\n";
            return 1;
        }
    
        if (total > 50) return 0;
    
        int sum = 0;
    
        for (int i = lastI; i < 6; i++)
        {
            v.push_back(coins[i]);
            sum += solve(total + coins[i], coins, i); 
            v.pop_back();
        }
    
        return sum;
    }
    
    
    int main()
    {
        int coins[6] = {2, 4, 6, 10, 15, 50};
        cout << solve(0, coins, 0) << endl;
    }
    

    A very dirty brute force solution that prints all possible combinations.

    This is a very famous problem, so try reading about better solutions others have provided.

    0 讨论(0)
  • 2020-12-25 08:59

    It's very similar to the knapsack problem

    0 讨论(0)
提交回复
热议问题