File tree

9 files changed

+159
-5
lines changed

9 files changed

+159
-5
lines changed
Some generated files are not rendered by default. Learn more about customizing how changed files appear on .
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
/**
44
* @Author: iqqcode
55
* @Date: 2020-10-01 16:25
6-
* @Description: 有4个线程分别获取C、D、E、F盘的大小,第5个线程统计总大小
6+
* @Description:[大华]
7+
* 有4个线程分别获取C、D、E、F盘的大小,第5个线程统计总大小
78
*/
89

910
import java.util.*;
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* @Author: iqqcode
77
* @Date: 2020-09-14 09:53
8-
* @Description: 滴滴笔试题一
8+
* @Description:[滴滴笔试题一]
99
*
1010
* 样例输入:
1111
* 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package duoyiwangluo;
2+
3+
import java.math.BigDecimal;
4+
import java.util.*;
5+
6+
/**
7+
* @Author: iqqcode
8+
* @Date: 2020-10-03 16:44
9+
* @Description:[多益网络]
10+
* 实现抢红包算法, 10元5个红包, 保证红包的最小金额为1
11+
*
12+
* [基础实现]:非公平, 剩余人的随机范围越来越小
13+
*/
14+
public class _01_RedPackage {
15+
16+
public static void main(String[] args) {
17+
Scanner in = new Scanner(System.in);
18+
int total = in.nextInt() * 100;
19+
in.nextLine();
20+
int amount = in.nextInt();
21+
int[] redPackage = new int[amount];
22+
getRandomMoney(total, amount, redPackage);
23+
// 换做分输出
24+
for (int i = 0; i < amount; i++) {
25+
System.out.println("抢到金额" + new BigDecimal(redPackage[i]).divide(new BigDecimal(100)));
26+
}
27+
}
28+
29+
public static void getRandomMoney(int total, int amount, int[] redPackage) {
30+
final int MIN_RED = 1;
31+
Arrays.fill(redPackage, MIN_RED);
32+
int last = total - amount;
33+
Random random = new Random();
34+
for (int i = 0; i < amount - 1; i++) {
35+
//rand.nextInt(MAX - MIN + 1) + MIN
36+
int randRedValue = random.nextInt(last - 1);
37+
redPackage[i] += randRedValue;
38+
last -= randRedValue;
39+
}
40+
redPackage[amount - 1] += last;
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package duoyiwangluo;
2+
3+
import java.math.BigDecimal;
4+
import java.util.Random;
5+
import java.util.Scanner;
6+
7+
/**
8+
* @Author: iqqcode
9+
* @Date: 2020-10-04 09:22
10+
* @Description:二倍均值法 [公式]
11+
* 剩余红包金额为M,剩余人数为N: 每次抢到的金额 = 随机区间 (0, M / N X 2)
12+
* <p>
13+
* [最后一次非公平] :
14+
* 除了最后一次,任何一次抢到的金额都要小于人均金额的两倍,并不是任意的随机。
15+
*/
16+
public class _02_RedPackage {
17+
public static void main(String[] args) {
18+
Scanner in = new Scanner(System.in);
19+
// 输入金额,分为单位, 扩大100倍
20+
int total = in.nextInt() * 100;
21+
in.nextLine();
22+
int amount = in.nextInt();
23+
int[] redPackage = new int[amount];
24+
getRandomMoney(total, amount, redPackage);
25+
// 换做分输出
26+
for (int i = 0; i < amount; i++) {
27+
System.out.println("抢到金额" + new BigDecimal(redPackage[i]).divide(new BigDecimal(100)));
28+
}
29+
}
30+
31+
public static void getRandomMoney(int total, int amount, int[] redPackage) {
32+
int last_money = total;
33+
int last_people = amount;
34+
Random random = new Random();
35+
for (int i = 0; i < amount - 1; i++) {
36+
//随机范围:[1,剩余人均金额的2倍 - 1] 分
37+
int randRedValue = random.nextInt(last_money / last_people * 2 - 1) + 1;
38+
redPackage[i] = randRedValue;
39+
last_money -= randRedValue;
40+
last_people--;
41+
}
42+
//最后一人分剩余金额, 此处为非公平
43+
redPackage[amount - 1] = last_money;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package duoyiwangluo;
2+
3+
import java.math.BigDecimal;
4+
import java.util.*;
5+
6+
/**
7+
* @Author: iqqcode
8+
* @Date: 2020-10-04 09:47
9+
* @Description:线段切割法 实现公平
10+
*/
11+
public class _03_RedPackage {
12+
//线段分割法
13+
private static List<Integer> getRandomMoney(int totalMoney, int amount) {
14+
//验证参数合理校验
15+
//为了使用random.nextInt(Integer)方法, 不得不先把红包金额放大100倍,最后在main函数里面再除以100
16+
//这样就可以保证每个人抢到的金额都可以精确到小数点后两位
17+
int redMoney = (int) (totalMoney * 100);
18+
if (redMoney < amount || redMoney < 1) {
19+
System.out.println("红包个数必须大于0,并且最小红包不少于1分");
20+
}
21+
List<Integer> boards = new ArrayList<>();
22+
boards.add(0);
23+
boards.add(redMoney);
24+
//红包个数和线段个数的关系
25+
while (boards.size() <= amount) {
26+
// 随机生成切割点
27+
int index = new Random().nextInt(redMoney - 1) + 1;
28+
if (boards.contains(index)) {
29+
//保证线段的位置不相同
30+
continue;
31+
}
32+
boards.add(index);
33+
}
34+
35+
//计算每个红包的金额,将两个板子之间的钱加起来
36+
Collections.sort(boards);
37+
List<Integer> list = new ArrayList<>();
38+
for (int i = 0; i < boards.size() - 1; i++) {
39+
Integer e = boards.get(i + 1) - boards.get(i);
40+
list.add(e);
41+
}
42+
return list;
43+
44+
}
45+
46+
public static void main(String[] args) {
47+
List<Integer> accountList = getRandomMoney(10, 5);
48+
BigDecimal count = new BigDecimal(0);
49+
for (Integer amount : accountList) {
50+
//将抢到的金额再除以100进行还原
51+
BigDecimal tmpcount = new BigDecimal(amount).divide(new BigDecimal(100));
52+
count = count.add(tmpcount);
53+
System.out.println("抢到金额:" + tmpcount);
54+
55+
}
56+
System.out.println("total = " + count);
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
/**
55
* @Author: iqqcode
66
* @Date: 2020-09-25 21:34
7-
* @Description:求质因数
7+
* @Description:[吉比特]
8+
* 求质因数
89
*/
910
public class Solution {
1011
public static void main(String[] args) {
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import java.util.*;
44

55
/**
6+
* @Author: iqqcode
7+
* @Date: 2020-09-25 21:34
8+
* @Description:[巨人网络]
69
* 两个字符串的最长公共子串
710
*/
8-
911
public class Main {
1012
public static void main(String[] args) {
1113
//"abccade","dgcadde"
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import java.util.Arrays;
44
import java.util.Scanner;
55

6+
/**
7+
* @Author: iqqcode
8+
* @Description:[美团]
9+
*/
610
public class Main {
711
/**
812
* 8 3

0 commit comments

Comments
 (0)