using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp5 { class Program { static void Main(string[] args) { #region 红包算法一 Console.WriteLine("-----------------------------------红包算法一---------------------"); //初始化要发起的红包基础数据 double total = 100; int num = 50; double min = 0.01; string temp; bool flag = false; Console.WriteLine(string.Format("是否需要自定义红包金额和数量(默认{0}元/{1}人)Y/N:", total, num)); temp = Console.ReadLine(); if (temp.Trim().ToLower().Equals("y") || temp.Trim().ToLower().Equals("yes")) { Console.WriteLine("请输入你要发起的红包金额:"); do { temp = Console.ReadLine(); flag = double.TryParse(temp, out total); if (!flag) { Console.WriteLine("金额必须为整数或小数,请重新输入:"); } } while (!flag); Console.WriteLine("请输入你要发起的红包个数:"); do { temp = Console.ReadLine(); flag = int.TryParse(temp, out num); if (!flag) { Console.WriteLine("红包个数必须为整数,请重新输入:"); } } while (!flag); } total -= min * num; if (total < 0) { Console.WriteLine("抱歉,你的金额不足!"); return; } //产生正态分布的随机红包金额,并计算相关的金额和数量保证数据的准确性 double average = total / num; double variance = 1; Random u1 = new Random(); Random u2 = new Random(); double[] nums = new double[num]; for (int i = 0; i < num; i++) { double? result = total; if (i < num - 1 && total > 0) { do { result = Round((double)Normal(u1.NextDouble(), u2.NextDouble(), average, variance), 2); } while (result == null || result < 0); if (total > result) { total = (double)Round((total - (double)result), 2); } else { result = total; total = 0; } } else if (i == num - 1) { total = 0; } nums[i] = Math.Round(min + (double)result, 2); //浮点运算问题,这里需要四舍五入数据才正确 Console.WriteLine(string.Format("第{0}个红包金额:{1}", i + 1, (min + result))); Console.WriteLine("剩余金额:" + ((i != num - 1 && total == 0) ? min * (num - i - 1) : total + (min * (num - i - 1)))); } Console.WriteLine("最大金额:" + nums.Max()); Console.WriteLine("最小金额:" + nums.Min()); Console.WriteLine("总额:" + Round(nums.Sum(), 2)); Console.WriteLine("初始方差:" + variance); Console.WriteLine("结果方差:" + Variance(nums)); Console.WriteLine("按任意键继续"); Console.ReadKey(); #endregion #region 红包算法二 Console.WriteLine("-----------------------------------红包算法二---------------------"); RedPackage pp = new RedPackage(); int packageNum = 15; pp.remainCount = packageNum; pp.remainMoney = 100; var list = GetMoneys(pp); Console.WriteLine("总金额 {0}", list.Sum()); #endregion Console.ReadKey(); } #region 算法一 ///
/// 产生符合正态分布的随机数 ///
/// 正态分布第一个随机数 /// 正态分布第二个随机数 /// 正态期望(平均值) /// 正态标准差(Math.Sqrt(方差)) /// public static double? Normal(double u1, double u2, double averageValue, double variance) { double? result = null; try { result = averageValue + Math.Sqrt(variance) * Math.Sqrt((-2) * Math.Log(u1)) * Math.Sin(2 * Math.PI * u2); } catch (Exception) { result = null; } return result; } ///
/// 求一组数据的方差 ///
/// 要求的数组 /// public static double Variance(double[] nums) { double average = nums.Sum() / nums.Length; double sum = 0; double variance = 0; foreach (double num in nums) { sum += Math.Pow((num - average), 2); } variance = sum / nums.Length; return variance; } ///
/// 截取小数指定小数位,且不四舍五入 ///
/// 要截取的小数 /// 截取小数后位数 /// public static double? Round(double originNum, int lastNum) { double? result = null; int index = originNum.ToString().IndexOf('.'); if (index != -1) { string temp = originNum.ToString(); result = Convert.ToDouble(temp.Substring(0, index + 1) + temp.Substring(index + 1, Math.Min(temp.Length - index - 1, lastNum))); } if (result == 0) { result = null; } else if (index == -1) { result = originNum; } return result; } #endregion #region 算法二 /// 红包算法 ///
/// /// public static List GetMoneys(RedPackage _redPacket) { //人均最小金额 double min = 0.01; if (_redPacket.remainMoney < _redPacket.remainCount * min) return null; int num = _redPacket.remainCount; List array = new List(); Random r = new Random(); for (int i = 0; i < num; i++) { if (_redPacket.remainCount == 1) { _redPacket.remainCount--; array.Add(Convert.ToDouble(_redPacket.remainMoney.ToString("0.00"))); Console.WriteLine(string.Format("第{0}个红包:{1}元", i + 1, Convert.ToDouble(_redPacket.remainMoney.ToString("0.0")))); } else { //(_redPacket.remainMoney - (_redPacket.remainCount - 1) * min):保存剩余金额可以足够的去分配剩余的红包数量 double max = (_redPacket.remainMoney - (_redPacket.remainCount - 1) * min) / _redPacket.remainCount * 2; double money = r.NextDouble() * max; money = Convert.ToDouble((money <= min ? min : money).ToString("0.00")); _redPacket.remainCount--; _redPacket.remainMoney -= money; array.Add(money); Console.WriteLine(string.Format("第{0}个红包:{1}元", i + 1, money)); } } //再次随机 return array.OrderBy(o => Guid.NewGuid()).ToList(); } public class RedPackage { ///
/// 剩余的红包数量 ///
public int remainCount { set; get; } ///
/// 剩余金额 ///
public double remainMoney { set; get; } } #endregion } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp5 { class Program { static void Main(string[] args) { Console.WriteLine("---------------红包算法三 按排名从多大小--------------"); RedPackage pp = new RedPackage(); pp.remainCount = 99; pp.remainMoney = 100; var list = GetMoneys(pp); Console.WriteLine("总金额 {0}", list.Sum()); Console.ReadKey(); } public static List GetMoneys(RedPackage _redPackage) { double totalMoney = _redPackage.remainMoney; int num = _redPackage.remainCount; List list = new List(); List rankList = new List(); #region 获取所有排名 for (int i = 0; i < num; i++) { int rank = num - i; //从后往前排名 rankList.Add(rank); } #endregion int k = rankList.Sum(); for (int i = 0; i < num; i++) { var p = Math.Round((double)(rankList[i]) / k, 6, MidpointRounding.ToEven); double money = Math.Round(totalMoney * p, 2, MidpointRounding.ToEven); _redPackage.remainCount--; _redPackage.remainMoney -= money; list.Add(money); Console.WriteLine(string.Format("第{0}个红包:{1}元", i + 1, money)); } return list.OrderBy(o => Guid.NewGuid()).ToList(); } #region 算法三 public class RedPackage { ///
/// 剩余的红包数量 ///
public int remainCount { set; get; } ///
/// 剩余金额 ///
public double remainMoney { set; get; } } #endregion } }