首页 > 代码库 > 人民币找零钱,返回最少张数

人民币找零钱,返回最少张数


/// <summary>
/// 找零钱,返回人民币总张数
/// </summary>
class MoneyNums
{
  /// <summary>
  /// 定义人民币类别值
  /// </summary>
  private decimal[] RMB = { 100, 50, 20, 10, 5, 1, 0.5m, 0.1m };

/// <summary>
/// 找零钱,返回人民币总张数
/// </summary>
/// <param name="retval">找零总值</param>
/// <returns>返回张数</returns>
public int GetNums(decimal retval)
{
  int Nums = 0;
  if (retval > 0)
  {
    for (int i = 0; i < RMB.Length; i++)
    {
      while (retval >= RMB[i])
      {
        retval -= RMB[i];
        Nums++;

        Console.WriteLine(RMB[i]);
      }
    }
  }

  return Nums;
  }
}

人民币找零钱,返回最少张数