正文
100块发30个红包
demo
////// 抢红包 /// /// /// ///public List qhb(int money, int num) { int min = 1; int pjs = money / num; List list = new List (); 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; }
////// 余数处理 /// /// /// ///public List checklist(int money, int pjs, List list) { if (money != 0) { if (money > 0) { List 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 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; }
////// 冒泡排序 /// /// /// ///public List maopao(List 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; }
////// 打乱顺序 /// /// ///public List suiji(List 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实现微信抢红包代码实例的详细内容,更多请关注其它相关文章!