Algorithm to determine coin combinations

后端 未结 13 2601
臣服心动
臣服心动 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: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 coinTypes;
    int value;
    

    and the outputs:

    set< map > 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.
    

提交回复
热议问题