zoukankan      html  css  js  c++  java
  • 分钱单算法

    分钱单算法

    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.5f0.2f0.1f };

            
    static void Main(string[] args)
            
    {
                
    float[] 工资单 = new float[] { 2104f, 2320f, 3450f, 4520.1f4876.3f4995.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[] 0000000000 };

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

        }

    }


    版权信息
    作者:Chinasf
    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    == 与 equals 之区别
    值传递和引用传递
    try-catch-finally容易犯的错误
    设计模式之备忘录模式
    设计模式之观察者模式 Observer
    设计模式之状态模式 State
    设计模式之模板方法模式 templateMethod
    设计模式之策略模式 Strategy
    Java过滤器与SpringMVC拦截器之间的关系与区别
    SpringMVC中使用Interceptor拦截器
  • 原文地址:https://www.cnblogs.com/Chinasf/p/958409.html
Copyright © 2011-2022 走看看