贪心的找零钱算法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;
}

posted @ 2010-04-06 10:43  一修先生  阅读(481)  评论(0编辑  收藏  举报