Find the least number of coins required that can make any change from 1 to 99 cents

后端 未结 27 2007
生来不讨喜
生来不讨喜 2020-12-07 10:08

Recently I challenged my co-worker to write an algorithm to solve this problem:

Find the least number of coins required that can make any change from

相关标签:
27条回答
  • 2020-12-07 10:40

    This might be a generic solution in C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace CoinProblem
    {
        class Program
        {
            static void Main(string[] args)
            {
                var coins = new int[] { 1, 5, 10, 25 }; // sorted lowest to highest
                int upperBound = 99;
                int numCoinsRequired = upperBound / coins.Last();
                for (int i = coins.Length - 1; i > 0; --i)
                {
                    numCoinsRequired += coins[i] / coins[i - 1] - (coins[i] % coins[i - 1] == 0 ? 1 : 0);
                }
                Console.WriteLine(numCoinsRequired);
                Console.ReadLine();
            }
        }
    }
    

    I haven't fully thought it through...it's too late at night. I thought the answer should be 9 in this case, but Gabe said it should be 10... which is what this yields. I guess it depends how you interpret the question... are we looking for the least number of coins that can produce any value <= X, or the least number of coins that can produce any value <= X using the least number of coins? For example... I'm pretty sure we can make any value with only 9 coins, without nickels, but then to produce 9... you need...oh... I see. You'd need 9 pennies, which you don't have, because that's not what we chose... in that case, I think this answer is right. Just a recursive implementation of Thomas' idea, but I don't know why he stopped there.. you don't need to brute force anything.

    Edit: This be wrong.

    0 讨论(0)
  • 2020-12-07 10:40

    Came across this one today, while studying https://www.coursera.org/course/bioinformatics

    DPCHANGE(money, coins)
     MinNumCoins(0) ← 0
     for m ← 1 to money
            MinNumCoins(m) ← ∞
            for i ← 1 to |coins|
                if m ≥ coini
                    if MinNumCoins(m - coini) + 1 < MinNumCoins(m)
                        MinNumCoins(m) ← MinNumCoins(m - coini) + 1
        output MinNumCoins(money)
    

    Takes a comma-separated string of denominations available, and the target amount.

    C# implementation:

        public static void DPCHANGE(int val, string denoms)
        {
            int[] idenoms = Array.ConvertAll(denoms.Split(','), int.Parse);
            Array.Sort(idenoms);
            int[] minNumCoins = new int[val + 1];
    
            minNumCoins[0] = 0;
            for (int m = 1; m <= val; m++)
            {
                minNumCoins[m] = Int32.MaxValue - 1;
                for (int i = 1; i <= idenoms.Count() - 1; i++)
                {
                    if (m >= idenoms[i])
                    {
                        if (minNumCoins[m - idenoms[i]] + 1 < minNumCoins[m])
                        {
                            minNumCoins[m] = minNumCoins[m - idenoms[i]] + 1;
                        }
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-07 10:40

    Example Program:

    #include<stdio.h> 
    
        #define LEN 9 // array length
        int main(){
            int coins[LEN]={0,0,0,0,0,0,0,0,0}; // coin count
            int cointypes[LEN]={1000,500,100,50,20,10,5,2,1}; // declare your coins and note here {ASC order}   
            int sum =0; //temp variable for sum
            int inc=0; // for loop
            int amount=0; // for total amount
            printf("Enter Amount :");
            scanf("%d",&amount);
            while(sum<amount){
                if((sum+cointypes[inc])<=amount){
                       sum = sum+  cointypes[inc];
                        //printf("%d[1] - %d\n",cointypes[inc],sum);
                        //switch case to count number of notes and coin
                       switch(cointypes[inc]){
                        case 1000:
                               coins[0]++;
                               break;
                        case 500:
                               coins[1]++;
                               break;
                        case 100:
                               coins[2]++;
                               break;
                        case 50:
                               coins[3]++;
                               break;               
                        case 20:
                               coins[4]++; 
                               break;
                        case 10:
                               coins[5]++;
                               break;
                        case 5:
                               coins[6]++;
                               break;
                        case 2:
                               coins[7]++;
                               break;
                        case 1:
                               coins[8]++;
                               break;
                           }
                    }else{
                       inc++;
                    }
                }
            printf("note for %d in\n note 1000 * %d\n note 500 * %d\n note 100 * %d\n note 50 * %d\n note 20 * %d\n note 10 * %d\n coin 5 * %d\n coin 2 * %d\n coin 1 * %d\n",amount,coins[0],coins[1],coins[2],coins[3],coins[4],coins[5],coins[6],coins[7],coins[8]); 
    
        }
    
    0 讨论(0)
提交回复
热议问题