分錢單演算法
1.有6個員工,每個人的工資是2000到5000不等,並且有零頭;
【1】2104
【2】2320
【3】3450
【4】4520.1
【5】4876.3
【6】4995.9
2.財務發現金,現求出共要發多少現金,100元、50元、20元、10元、5元、2元、1元、5角、2角、1角分別為多少?(分不記)
3.規則要求,按大面額現金最優發放。
具體的演算法:
using System;
using System.Collections.Generic;
using System.Text;
namespace 分錢單
{
class Program
{
//人民幣民面額
static float[] 面額 = new float[] { 100f, 50f, 20f, 10f, 5f, 2f, 1f, 0.5f, 0.2f, 0.1f };
static void Main(string[] args)
{
float[] 工資單 = new float[] { 2104f, 2320f, 3450f, 4520.1f, 4876.3f, 4995.9f };
int[] 面額計數 = 分錢單計算(工資單);
float 總金額 = 總金額計算(工資單);
for (int i = 0; i < 面額計數.Length; i++)
Console.WriteLine("面額{0}元 共 {1}張", 面額[i], 面額計數[i]);
Console.WriteLine("共{0}元", 總金額);
Console.WriteLine("第一個人的工資:{0} ", 工資單[0]);
面額計數 = 分錢單計算(new float[] { 工資單[0] });
for (int i = 0; i < 面額計數.Length; i++)
Console.WriteLine("面額{0}元 共 {1}張", 面額[i], 面額計數[i]);
Console.ReadLine();
}
static int floatHelper(float f)
{
return int.Parse((f * 100).ToString());
}
static int[] 分錢單計算(float[] 金額)
{
int[] 面額計數器 = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
foreach (float tm in 金額)
{
int iTm = floatHelper(tm);
while (iTm > 0)
for (int i = 0; i < 面額.Length; i++)
if (iTm >= floatHelper(面額[i]))
{
iTm -= floatHelper(面額[i]);
面額計數器[i] += 1;
break;
}
}
return 面額計數器;
}
static float 總金額計算(float[] 金額)
{
float totalMoney = 0.00f;
foreach (float tm in 金額)
totalMoney += tm;
return totalMoney;
}
}
}