Skip to content

Commit ad743af

Browse files
committed
红包算法
1 parent e774b6c commit ad743af

3 files changed

Lines changed: 162 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.algorithm.study.demo.algorithm;
2+
3+
/**
4+
* @Author: liuxun
5+
* @CreateDate: 2018/11/8 下午1:50
6+
* @Version: 1.0
7+
*/
8+
public class MainDemo {
9+
public static void main(String[] args) {
10+
for (int i=0;i<100;i++){
11+
double redPacket = (Math.random() * (0.5 - 0.1) + 0.1);
12+
System.out.println(redPacket);
13+
}
14+
System.out.println((int)(Math.random()*0.9+0.1));
15+
}
16+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.algorithm.study.demo.algorithm;
2+
3+
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
/**
7+
*
8+
* 模拟微信红包生成,以分为单位
9+
* @Author: liuxun
10+
* @CreateDate: 2018/11/8 下午2:22
11+
* @Version: 1.0
12+
*/
13+
public class RedPacket {
14+
15+
/**
16+
* 生成红包最小值 1分
17+
*/
18+
private static final int MIN_MONEY = 1;
19+
20+
/**
21+
* 生成红包最大值 200人民币
22+
*/
23+
private static final int MAX_MONEY = 200 * 100;
24+
25+
/**
26+
* 小于最小值
27+
*/
28+
private static final int LESS = -1;
29+
/**
30+
* 大于最大值
31+
*/
32+
private static final int MORE = -2;
33+
34+
/**
35+
* 正常值
36+
*/
37+
private static final int OK = 1;
38+
39+
/**
40+
* 最大的红包是平均值的 TIMES 倍,防止某一次分配红包较大
41+
*/
42+
private static final double TIMES = 2.1F;
43+
44+
private int recursiveCount = 0;
45+
46+
public List<Integer> splitRedPacket(int money, int count) {
47+
List<Integer> moneys = new LinkedList<>();
48+
49+
//金额检查,如果最大红包 * 个数 < 总金额;则需要调大最小红包 MAX_MONEY
50+
if (MAX_MONEY * count <= money) {
51+
System.err.println("请调大最小红包金额 MAX_MONEY=[" + MAX_MONEY + "]");
52+
return moneys ;
53+
}
54+
55+
56+
//计算出最大红包
57+
int max = (int) ((money / count) * TIMES);
58+
max = max > MAX_MONEY ? MAX_MONEY : max;
59+
60+
for (int i = 0; i < count; i++) {
61+
//随机获取红包
62+
int redPacket = randomRedPacket(money, MIN_MONEY, max, count - i);
63+
moneys.add(redPacket);
64+
//总金额每次减少
65+
money -= redPacket;
66+
}
67+
68+
return moneys;
69+
}
70+
71+
private int randomRedPacket(int totalMoney, int minMoney, int maxMoney, int count) {
72+
//只有一个红包直接返回
73+
if (count == 1) {
74+
return totalMoney;
75+
}
76+
77+
if (minMoney == maxMoney) {
78+
return minMoney;
79+
}
80+
81+
//如果最大金额大于了剩余金额 则用剩余金额 因为这个 money 每分配一次都会减小
82+
maxMoney = maxMoney > totalMoney ? totalMoney : maxMoney;
83+
84+
//在 minMoney到maxMoney 生成一个随机红包
85+
int redPacket = (int) (Math.random() * (maxMoney - minMoney) + minMoney);
86+
87+
int lastMoney = totalMoney - redPacket;
88+
89+
int status = checkMoney(lastMoney, count - 1);
90+
91+
//正常金额
92+
if (OK == status) {
93+
return redPacket;
94+
}
95+
96+
//如果生成的金额不合法 则递归重新生成
97+
if (LESS == status) {
98+
recursiveCount++;
99+
System.out.println("recursiveCount==" + recursiveCount);
100+
return randomRedPacket(totalMoney, minMoney, redPacket, count);
101+
}
102+
103+
if (MORE == status) {
104+
recursiveCount++;
105+
System.out.println("recursiveCount===" + recursiveCount);
106+
return randomRedPacket(totalMoney, redPacket, maxMoney, count);
107+
}
108+
109+
return redPacket;
110+
}
111+
112+
/**
113+
* 校验剩余的金额的平均值是否在 最小值和最大值这个范围内
114+
*
115+
* @param lastMoney
116+
* @param count
117+
* @return
118+
*/
119+
private int checkMoney(int lastMoney, int count) {
120+
double avg = lastMoney / count;
121+
if (avg < MIN_MONEY) {
122+
return LESS;
123+
}
124+
125+
if (avg > MAX_MONEY) {
126+
return MORE;
127+
}
128+
129+
return OK;
130+
}
131+
132+
133+
public static void main(String[] args) {
134+
RedPacket redPacket = new RedPacket();
135+
List<Integer> redPackets = redPacket.splitRedPacket(10, 10);
136+
System.out.println(redPackets);
137+
138+
int sum = 0;
139+
for (Integer red : redPackets) {
140+
sum += red;
141+
}
142+
System.out.println(sum);
143+
}
144+
145+
}

src/main/java/com/algorithm/study/demo/algorithm/ZumaProject.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static void main(String[] args) {
3131
String findInput=find(index,newInput);
3232
System.out.println("消除后的结果:"+findInput);
3333

34+
3435
}
3536
public static String matcher(String input)
3637
{

0 commit comments

Comments
 (0)