que red package

master
robin 3 years ago
parent e90ac4edbe
commit e224fbae0c

@ -0,0 +1,74 @@
package org.alis.smallcc.algorithm.leetcode;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
/**
*
* house-robber
*
*
* <p>
* [1,2,2,4,1,2,3] = 4+3+2
*
* @author robin
* @date 2022/1/29 15:35
*/
public class LeetCode16 {
private static final Integer[] ARRAY = {3, 1, 5, 7, 9, 11};
public static void main(String[] args) {
// 生成别墅
// Integer[] array = new Integer[10];
// 给每个别墅随机加钱
// for (int i = 0; i < array.length; i++) {
// array[i] = ThreadLocalRandom.current().nextInt(0, 100);
// }
// 打印出别墅的排列
System.out.println(Arrays.toString(ARRAY));
// 小偷开始偷钱
Integer robberies = robberies(ARRAY);
// 小偷一共偷了多少钱
System.out.println(robberies);
}
/**
* @param array
* @return
*/
private static Integer robberies(Integer[] array) {
// 小偷偷的钱的总额
int count = 0;
for (int i = 0; i < array.length; i += 2) {
int max = Math.max(array[i], i < array.length - 1 ? array[i + 1] : array[i]);
System.out.printf("%d + %d = %d%n", count, max, count + max);
count += max;
}
return count;
}
/**
*
*
* @param array
* @return
*/
// private static Integer robberies(Integer[] array) {
// int a = 0;
// int b = 0;
//
// for (Integer integer : array) {
// int temp = b;
// b = Math.max(a + integer, b);
// System.out.printf("%d + %d = %d%n", a, integer, a + integer);
// a = temp;
// }
//
// return b;
// }
}

@ -0,0 +1,48 @@
package org.alis.smallcc.algorithm.redpackage;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
/**
*
*
* <p>
* 100
*
* @author robin
* @date 2022/2/7 10:22
*/
public class DoubleDivide {
public static void main(String[] args) {
dividePackage(10000, 10);
}
/**
*
*
* @param total
* @param count
*/
private static void dividePackage(Integer total, Integer count) {
int temp = count;
int endCount = 0;
int min = 1;
for (int i = 1; i <= temp; i++) {
int amount;
if (i == temp) {
amount = total;
} else {
amount = Math.max(min, ThreadLocalRandom.current().nextInt(total / count * 2 - 1) + 1 - min);
}
total -= amount;
count--;
System.out.printf("第%d 个人抢到了 %f 元%n", i, (float) amount / 100);
endCount += amount;
}
System.out.printf("红包的总数是%d %n", endCount);
}
}

@ -0,0 +1,31 @@
package org.alis.smallcc.algorithm.redpackage;
import java.util.concurrent.ThreadLocalRandom;
/**
*
*
*
* @author robin
* @date 2022/2/7 10:54
*/
public class RandomRedPackage {
public static void main(String[] args) {
random(10000, 10);
}
private static void random(Integer total, Integer count) {
int min = 1;
for (int i = 1; i <= count; i++) {
int amount;
if (i == count) {
amount = total;
} else {
amount = Math.max(min, ThreadLocalRandom.current().nextInt(total - 1) + 1 - min);
}
total -= amount;
System.out.printf("第%d 个人抢到了 %f 元%n", i, (float) amount / 100);
}
}
}

@ -0,0 +1,61 @@
package org.alis.smallcc.algorithm.redpackage;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 线
*
* @author robin
* @date 2022/2/7 11:32
*/
public class SegmentRedPackage {
public static void main(String[] args) {
List<Integer> integers = lineCut(10000, 10);
}
private static List<Integer> lineCut(int money, int people) {
if (money < 1 || people < 1 || money < people) {
return Collections.emptyList();
}
List<Integer> team = new ArrayList<>(people - 1);
List<Integer> result = new ArrayList<>(people);
Random random = new Random();
while (team.size() < people - 1) {
int randomMoney = random.nextInt(money) + 1;
if (!team.contains(randomMoney)) {
team.add(randomMoney);
}
}
Collections.sort(team);
System.out.print("分割点:");
System.out.println(team.stream().map(Integer::floatValue).map(f->f/100).collect(Collectors.toList()));
int left = 0;
for (Integer integer : team) {
result.add(integer - left);
left = integer;
}
result.add(money - left);
System.out.print("每人金额:");
System.out.println(result.stream().map(Integer::floatValue).map(f->f/100).collect(Collectors.toList()));
// 验证分割后的数是否是输入的总金额
result.stream().reduce(Integer::sum).ifPresent(amount -> {
System.out.print("总金额:");
System.out.println(amount);
});
return result;
}
}
Loading…
Cancel
Save