Greedy algorithm to solve the problem of the change coins

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Coin
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] value = new int[] { 1, 2, 5, 10, 20, 50, 100 };
            int[] count = new int[] { . 3 , 0 , 2 , . 1 , 0 , . 3 , . 5 };
             int [] = Result new new  int [count.Length + . 1 ]; // will use an amount of money for each denomination is recorded, the last record the change case 
            CoinMoneny ( 600 , value, COUNT, Result); // 600 blocks the change 
            the foreach ( var Item in Result) // the case where the output change due 
            { 
                Console.WriteLine (Item); 
            } 
            the Console.ReadKey () ;
        }
        /// <summary>
        /// 对货币进行找零
        /// </summary>
        /// <param name="money"></param>
        /// <param name="value"></param>
        /// <param name="count"></param>
        /// <param name="result"></param>
        public static void CoinMoneny(int money,int[] value,int[] count,int[] result)
        {
            if (money <= int
            };return            {
                first determine whether there is a need to change due//)0


             temp = value.Length - 1; // start with the largest denomination for change for 
            the while ( to true ) 
            { 
                IF (Money> value [TEMP] * COUNT [TEMP]) // Maximum number of the change is less than the total number of denominations 
                { 
                    Result [TEMP] = COUNT [TEMP]; // all recorded maximum number of all denominations into an array 
                    Money - value = [TEMP] * COUNT [TEMP]; // find the maximum number of coins of the denomination after use 
                }
                 the else // maximum denomination is greater than the total number of the change number 
                { 
                    Result [TEMP] = Money / value [TEMP]; // accordance with the change amount of the actual recording 
                    money - = result [temp] * value [temp];// remaining number change due
                } 
                TEMP = TEMP - . 1 ; // detection in a coin denomination 
                IF (TEMP < 0 || Money <= 0 ) // termination find 
                {
                     BREAK ; 
                } 
            } 
        } 
       
    } 
}

 

Guess you like

Origin www.cnblogs.com/zhangyang4674/p/11319931.html