這篇文章主要介紹如何使用ASP.NET實(shí)現(xiàn)微信搶紅包,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)建站是一家專注于網(wǎng)站建設(shè)、成都做網(wǎng)站與策劃設(shè)計,通州網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:通州等地區(qū)。通州做網(wǎng)站價格咨詢:18982081108正文
100塊發(fā)30個紅包
50塊發(fā)13個紅包
1塊發(fā)10個紅包
發(fā)紅包需要滿足以下幾個條件
1.總金額不變
2.每個紅包都必須有錢
3.盡量的均勻點(diǎn),不然搶紅包沒什么意思了
實(shí)現(xiàn)思路
1.首先要確定最小單位,這里是精確到分,我這里以int類型進(jìn)行計算,得出的結(jié)果也全是int類型
2.數(shù)據(jù)均勻,這里以 1
4.打亂順序,細(xì)心的園友可能發(fā)現(xiàn),紅包是有順序規(guī)律的,我們必須簡單的寫個function去打亂它.
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; } //前面數(shù)據(jù)都是合理的,結(jié)尾必須多退少補(bǔ) list = checklist(money, pjs, list); return list; }
////// 余數(shù)處理 /// /// /// ///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實(shí)現(xiàn)微信搶紅包”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!