zoukankan      html  css  js  c++  java
  • 固定金额和人数红包分配算法

    转自:http://www.cnblogs.com/wicub/p/6096897.html
     
    /// <summary>
            /// 生成红包数组
            /// </summary>
            /// <param name="totalMoney">总金额</param>
            /// <param name="perMax">最大金额</param>
            /// <param name="perMin">最小金额</param>
            /// <param name="totalUser">总人数</param>
            static decimal[] CalueHB(int totalMoney, decimal perMax, decimal perMin, int totalUser)
            {
                int i = 0; //第几人
                decimal[] array = new decimal[totalUser];//分配结果集            
                Random ran = new Random();
    
                for (i = 0; i < totalUser; i++) //保证每个人有最小金额+随机值
                {
                    array[i] = perMin + Math.Round((Convert.ToDecimal(ran.NextDouble()) * (totalMoney / totalUser - perMin - 1)), 2);
                }
    
                decimal yet = array.Sum(); // 已分配的总金额
                decimal thisM = 0M; //当前随机分配金额
    
                while (yet < totalMoney)
                {
                    thisM = Math.Round((Convert.ToDecimal(ran.NextDouble()) * (perMax - perMin - 1)), 2);
    
                    i = ran.Next(0, totalUser); //随机选择人
                    if (yet + thisM > totalMoney)
                    {
                        thisM = totalMoney - yet;
                    }
    
                    if (array[i] + thisM < perMax)//判断是否超出最大金额
                    {
                        array[i] += thisM;
                        yet += thisM;
                    }
                }
    
    
                Array.Sort(array);
    
                yet = 0;
                for (i = 0; i < totalUser; i++)
                {
                    yet += array[i];
                    Console.Write("第{0}人=>分配{1}元,合计分配{2}元
    ", (i + 1).ToString().PadLeft(3, ' '), array[i].ToString("f2").PadLeft(8, ' '), yet.ToString("f2").PadLeft(8, ' '));
                }
    
                return array;
            }
    
  • 相关阅读:
    中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结
    各种求逆元
    A*(A_star)搜索总结
    线段树总结
    C++的STL
    Unable to make the session state request to the session state server处理方法
    判断UserAgent是否来自微信
    VS2010 EntityFramework Database First
    VS2010类似Eclipse文件查找功能-定位到
    Newtonsoft.Json随手记
  • 原文地址:https://www.cnblogs.com/jiang_zheng/p/6294261.html
Copyright © 2011-2022 走看看