在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
群里都在玩抢红包,抢了再发,发了再抢,简直是无聊,程序员感兴趣是的如何实现,这里简单说说实现思路,附上dome,代码有点low,好在是实现了,具体内容如下 正文 100块发30个红包
50块发13个红包
1块发10个红包
发红包需要满足以下几个条件 1.总金额不变 实现思路 demo /// <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; } /// <summary> /// 余数处理 /// </summary> /// <param name="money"></param> /// <param name="list"></param> /// <returns></returns> public List<int> checklist(int money, int pjs, List<int> list) { if (money != 0) { if (money > 0) { List<int> list_order = maopao(list); //给最小 if (money / pjs == 0) { list_order[0] += money; } else { for (int i = 0; i < money / pjs + 1; i++) { if (i == money / pjs) { list_order[i] += (money - (money / pjs) * pjs); } else { list_order[i] += pjs; } } } return list_order; } else { List<int> list_order = maopao(list,"desc"); //给最大 if (money / pjs == 0) { list_order[0] += money; } else { for (int i = 0; i < -(money / pjs) + 1; i++) { if (i == -(money / pjs)) { list_order[i] += (money - (money / pjs) * pjs); } else { list_order[i] -= pjs; } } } return list_order; } } return list; } /// <summary> /// 冒泡排序 /// </summary> /// <param name="list"></param> /// <param name="order"></param> /// <returns></returns> public List<int> maopao(List<int> list,string order = "asc") { if (order != "asc") { for (int i = 0; i < list.Count; i++) { for (int j = 0; j < list.Count - i - 1; j++) { if (list[j + 1] > list[j]) { int temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } } else { for (int i = 0; i < list.Count; i++) { for (int j = 0; j < list.Count-i-1;j++) { if (list[j+1] < list[j]) { int temp = list[j]; list[j] = list[j+1]; list[j+1] = temp; } } } } return list; } /// <summary> /// 打乱顺序 /// </summary> /// <param name="list"></param> /// <returns></returns> public List<int> suiji(List<int> list) { Random rnd = new Random(); for (int i = 0; i < list.Count; i++) { int temp = list[i]; int j = rnd.Next(0, list.Count-1); list[i] = list[j]; list[j] = temp; } return list; } 本文已被整理到了《ASP.NET微信开发教程汇总》,欢迎大家学习阅读。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持极客世界。 |
请发表评论