分錢單演算法

來源:互聯網
上載者:User

分錢單演算法

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;
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.