說到算法
一
商場買東西的時候
二
實現如下
代碼
class Program
{
//人民幣面額
private static decimal[] moneyArr = new decimal[] {
//存各種面額的人民幣發放份數
private static int[] moneyCountList = null;
static void Main(string[] args)
{
decimal currentMoney =
int[] moneyCount = Calculate(currentMoney);
Console
for (int i =
Console
Console
}
/// <summary>
/// 計算發放金額數
/// </summary>
/// <param name=
/// <returns></returns>
static int[] Calculate(decimal money)
{
moneyCountList = new int[] {
int tmpMoney = NumHelper(money);
while (tmpMoney >
{
for (int i =
{
if (tmpMoney >= NumHelper(moneyArr[i]))
{
tmpMoney
moneyCountList[i] +=
break;
}
}
}
return moneyCountList;
}
/// <summary>
/// 將金錢轉換成整數處理
/// </summary>
/// <param name=
/// <returns></returns>
static int NumHelper(decimal money)
{
return Convert
}
}
如你所看到的那樣
當然你可能會說加減沒有乘除來的快
代碼
class Program
{
//人民幣面額
private static decimal[] moneyArr = new decimal[] {
//存各種面額的人民幣發放份數
private static int[] moneyCountList = null;
static void Main(string[] args)
{
decimal currentMoney =
int[] moneyCount = Calculate(currentMoney);
Console
for (int i =
Console
Console
}
/// <summary>
/// 計算發放金額數
/// </summary>
/// <param name=
/// <returns></returns>
static int[] Calculate(decimal money)
{
moneyCountList = new int[] {
int tmpMoney = NumHelper(money);
while (tmpMoney >
{
for (int i =
{
if (tmpMoney >= NumHelper(moneyArr[i]))
{
int result = tmpMoney / NumHelper(moneyArr[i]); //直接除
moneyCountList[i] = result;//對應的發送份數
tmpMoney = tmpMoney % NumHelper(moneyArr[i]); //余數
break;
}
}
}
return moneyCountList;
}
/// <summary>
/// 將金錢轉換成整數處理
/// </summary>
/// <param name=
/// <returns></returns>
static int NumHelper(decimal money)
{
return Convert
}
}
其實這就是一個簡單數學問題的計算機語言(c#)描述而已(當然
From:http://tw.wingwit.com/Article/program/net/201311/13853.html