Home > Java > javaTutorial > body text

Detailed introduction to Java WeChat red envelope implementation algorithm

黄舟
Release: 2017-03-07 10:33:17
Original
1804 people have browsed it

This article mainly introduces the Java WeChat red envelope implementation algorithm in detail, and lists the core algorithm of the red envelope, which has a certain reference value. Interested friends can refer to it

With the current micro WeChat is becoming more and more popular, so more and more people are studying WeChat. Not long ago, our company asked me to create a questionnaire survey and red envelope function in the WeChat public account. After a period of research, the function was completed. The main implementation steps are all based on the WeChat public account development documents, which are very detailed. In the whole process, only the red envelope algorithm needs to be written carefully, because after all, money is involved, so you have to be careful, and not only do you need to send red envelopes in WeChat, we also When making an APP, you may also encounter the function of sending red envelopes, so the core algorithm of red envelopes is listed here for everyone to study and study together.

public static List getRed(int number,float total,double min){
  //红包数 
  //int number = 300; 
  //红包总额 
 // float total = 500; 
  float money; 
  //最小红包 
  //double min = 0.48; 
  double max; 
  int i = 1; 
  List math = new ArrayList(); 
  DecimalFormat df = new DecimalFormat("###.##"); 
  while (i < number) { 
  //保证即使一个红包是最大的了,后面剩下的红包,每个红包也不会小于最小值 
  max = total - min * (number - i); 
  int k = (int)(number - i) / 2; 
  //保证最后两个人拿的红包不超出剩余红包 
  if (number - i <= 2) { 
   k = number - i; 
   } 
  //最大的红包限定的平均线上下 
  max = max / k; 
  //保证每个红包大于最小值,又不会大于最大值 
  money = (int) (min * 100 + Math.random() * (max * 100 - min * 100 + 1)); 
  money = (float)money / 100; 
  //保留两位小数 
  money = Float.parseFloat(df.format(money)); 
  total=(int)(total*100 - money*100); 
  total = total/100; 
  math.add(money); 
  System.out.println("第" + i + "个人拿到" + money + "剩下" + total); 
  i++; 
  //最后一个人拿走剩下的红包 
  if (i == number) { 
   math.add(total); 
   System.out.println("第" + i + "个人拿到" + total + "剩下0"); 
   } 
  }
  //取数组中最大的一个值的索引 
  System.out.println("本轮发红包中第" + (math.indexOf(Collections.max(math)) + 1) + "个人手气最佳");
 return math; 
  }
Copy after login

The above is the detailed introduction of the java WeChat red envelope implementation algorithm. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!