贪心的找零钱算法c#版

要求对任意金额的零钱(从1美分到99美分),算出应该给出多少个面值分别为25美分(quarter)、10美分(dime)、5美分(nickel)和1美分(pennies)的硬币。

 

/// <summary>
/// 
/// <param name="aCoins">the little coins we have;such as {25,10,5,1}that must be sorted descending</param>
/// <param name="aNeed">which amount of money to be computed</param>
/// <returns>the every coin numbers needed</returns>
/// </summary>
public static int[] ComputeCoins(int[] aCoins, int aNeed)
        {
            int k=aCoins.Length;
            int[] result=new int[k];
            for(int i=0;i<k;i++)
           {
               result[i]=aNeed/aCoins[i];
               aNeed=aNeed%aCoins[i];
           }
           return result;
}

作者:一修先生
         
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/1971ruru/p/1705137.html