首頁  >  文章  >  微信小程式  >  ASP.NET實作微信搶紅包程式碼實例

ASP.NET實作微信搶紅包程式碼實例

Y2J
Y2J原創
2017-04-26 14:43:281943瀏覽

群組裡都在玩搶紅包,搶了再發,發了再搶,簡直是無聊,程式設計師感興趣是的如何實現,這裡簡單說說實現思路,附上dome,程式碼有點low,好在實現了,具體內容如下 

正文

100塊發送30個紅包

  

50塊發送13個紅包

#  

#1塊發10個紅包





#發紅包需要滿足以下幾個條件

1.總金額不變
2.每個紅包都必須有錢
3.盡量的均勻點,不然搶紅包沒什麼意思了 

####實現想法#########1.首先要確定最小單位,這裡是精確到分,我這裡以int類型進行計算,得出的結果也全是int型別###2.資料均勻,這裡以  1/// <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實作微信搶紅包程式碼實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn