前言
群里都在玩抢红包,抢了再发,发了再抢,简直是无聊,程序员感兴趣是的如何实现,这里简单说说实现思路,附上dome,代码有点low,好在是实现了,感兴趣的园友可以完善下,觉得还行的可以点个赞
正文
直接进入主题,先附上几张效果图(缺陷)
====================100块发30个红包============================
====================50块发13个红包============================
====================1块发10个红包============================
=========================================================
发红包需要满足以下几个条件
1.总金额不变
2.每个红包都必须有钱
3.尽量的均匀点,不然抢红包没什么意思了
=========================================================
实现思路
1.首先要确定最小单位,这里是精确到分,我这里以int类型进行计算,得出的结果也全是int类型
2.数据均匀,这里以 1<n<(剩余金额/剩余红包数)*2,这里防止一个红包抢走大量的钱,这里需要写个递归,去检查数据值是否过大,50元发13个红包的第3图,就是因为没有检查数据,导致钱不平均,这个可以根据自己的玩法去设置.
3.余数处理,当数据平均给20个红包,肯定跟总金额有所偏差,这时候我们就要多退少补,如果大于总金额,让红包金额多的去减少(总金额/总人数),如果还有剩余,就让第二多的去减少,直到补完这个空缺为止,反之亦然.
4.打乱顺序,细心的园友可能发现,红包是有顺序规律的,我们必须简单的写个function去打乱它.
=========================================================
demo
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/// <summary> /// 抢红包 /// </summary> /// <param name="money"></param> /// <param name="num"></param> /// <returns></returns> public List<int> qhb(int money, int num) { int min = 1; int pjs = money / num; List<int> list = new List<int>(); Random rnd = new Random(); for (int i = 0; i < num; i++) { int max = (money / (num - i)) * 2; int s = rnd.Next(min, max); s = checkmoney(s, min, max); list.Add(s); money -= s; } //前面数据都是合理的,结尾必须多退少补 list = checklist(money, pjs, list); return list; }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /// <summary> 2 /// 获取到合理数据 3 /// </summary> 4 /// <param name="i"></param> 5 /// <param name="min"></param> 6 /// <param name="max"></param> 7 /// <returns></returns> 8 public int checkmoney(int i, int min, int max) 9 { 10 if (i > max || i < min) 11 { 12 Random rnd = new Random(); 13 i = rnd.Next(min, max); 14 return checkmoney(i, min, max); 15 } 16 return i; 17 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /// <summary> 2 /// 余数处理 3 /// </summary> 4 /// <param name="money"></param> 5 /// <param name="list"></param> 6 /// <returns></returns> 7 public List<int> checklist(int money, int pjs, List<int> list) 8 { 9 if (money != 0) 10 { 11 if (money > 0) 12 { 13 14 List<int> list_order = maopao(list); 15 //给最小 16 if (money / pjs == 0) 17 { 18 list_order[0] += money; 19 } 20 else 21 { 22 for (int i = 0; i < money / pjs + 1; i++) 23 { 24 if (i == money / pjs) 25 { 26 list_order[i] += (money - (money / pjs) * pjs); 27 } 28 else 29 { 30 list_order[i] += pjs; 31 } 32 33 } 34 } 35 return list_order; 36 } 37 else 38 { 39 40 List<int> list_order = maopao(list,"desc"); 41 //给最大 42 if (money / pjs == 0) 43 { 44 45 list_order[0] += money; 46 } 47 else 48 { 49 for (int i = 0; i < -(money / pjs) + 1; i++) 50 { 51 if (i == -(money / pjs)) 52 { 53 list_order[i] += (money - (money / pjs) * pjs); 54 } 55 else 56 { 57 list_order[i] -= pjs; 58 } 59 60 } 61 } 62 return list_order; 63 } 64 } 65 return list; 66 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /// <summary> 2 /// 冒泡排序 3 /// </summary> 4 /// <param name="list"></param> 5 /// <param name="order"></param> 6 /// <returns></returns> 7 public List<int> maopao(List<int> list,string order = "asc") 8 { 9 if (order != "asc") 10 { 11 for (int i = 0; i < list.Count; i++) 12 { 13 for (int j = 0; j < list.Count - i - 1; j++) 14 { 15 if (list[j + 1] > list[j]) 16 { 17 int temp = list[j]; 18 list[j] = list[j + 1]; 19 list[j + 1] = temp; 20 21 } 22 } 23 } 24 } 25 else 26 { 27 for (int i = 0; i < list.Count; i++) 28 { 29 for (int j = 0; j < list.Count-i-1;j++) 30 { 31 if (list[j+1] < list[j]) 32 { 33 int temp = list[j]; 34 list[j] = list[j+1]; 35 list[j+1] = temp; 36 37 } 38 } 39 } 40 } 41 return list; 42 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /// <summary> 2 /// 打乱顺序 3 /// </summary> 4 /// <param name="list"></param> 5 /// <returns></returns> 6 public List<int> suiji(List<int> list) 7 { 8 Random rnd = new Random(); 9 for (int i = 0; i < list.Count; i++) 10 { 11 int temp = list[i]; 12 int j = rnd.Next(0, list.Count-1); 13 list[i] = list[j]; 14 list[j] = temp; 15 } 16 return list; 17 }