红包平均分配算法
2015-06-24 15:14
309 查看
直接上代码
import java.util.Iterator;
import java.util.TreeSet;
public class Hongbao {
private final int[] moneyList;
private final int number;
private final int totalMoney;
private int cur;
public Hongbao(int number, int totalMoney) {
if (number > totalMoney)
throw new IllegalArgumentException("number <= totalMoney");
this.number = number;
this.totalMoney = totalMoney;
moneyList = new int[number + 1];
TreeSet<Integer> moneySet = new TreeSet<>();
initial(moneySet);
int i = -1;
Iterator<Integer> iter = moneySet.iterator();
while (iter.hasNext()) {
moneyList[++i] = iter.next();
}
cur = 0;
}
protected void initial(TreeSet<Integer> moneySet) {
moneySet.add(0);
moneySet.add(totalMoney);
for (int i = 1; i < number; ++i) {
int tempMoney = rand(totalMoney);
while (moneySet.contains(tempMoney)) {
++tempMoney;
tempMoney %= totalMoney;
}
moneySet.add(tempMoney);
}
}
protected int rand(int n) {
return (int)(Math.random() * (n - 1) + 1);
}
public int getOne() {
return moneyList[++cur] - moneyList[cur - 1];
}
}测试代码
public static void main(String[] args) {
int[] arr = new int[20];
for (int i = 0; i < 20; i++)
arr[i] = 0;
for (int j = 0; j < 4000; ++j) {
Hongbao h = new Hongbao(20, 1000);
for (int i = 0; i < 20; ++i)
arr[i] += h.getOne();
}
for (int i = 0; i < 20; i++)
System.out.println(arr[i]);
}我测试的结果
198741
198305
197537
195746
196592
198304
202119
198655
200099
200290
198616
205446
198713
200420
203392
199065
205758
200732
198460
203010
import java.util.Iterator;
import java.util.TreeSet;
public class Hongbao {
private final int[] moneyList;
private final int number;
private final int totalMoney;
private int cur;
public Hongbao(int number, int totalMoney) {
if (number > totalMoney)
throw new IllegalArgumentException("number <= totalMoney");
this.number = number;
this.totalMoney = totalMoney;
moneyList = new int[number + 1];
TreeSet<Integer> moneySet = new TreeSet<>();
initial(moneySet);
int i = -1;
Iterator<Integer> iter = moneySet.iterator();
while (iter.hasNext()) {
moneyList[++i] = iter.next();
}
cur = 0;
}
protected void initial(TreeSet<Integer> moneySet) {
moneySet.add(0);
moneySet.add(totalMoney);
for (int i = 1; i < number; ++i) {
int tempMoney = rand(totalMoney);
while (moneySet.contains(tempMoney)) {
++tempMoney;
tempMoney %= totalMoney;
}
moneySet.add(tempMoney);
}
}
protected int rand(int n) {
return (int)(Math.random() * (n - 1) + 1);
}
public int getOne() {
return moneyList[++cur] - moneyList[cur - 1];
}
}测试代码
public static void main(String[] args) {
int[] arr = new int[20];
for (int i = 0; i < 20; i++)
arr[i] = 0;
for (int j = 0; j < 4000; ++j) {
Hongbao h = new Hongbao(20, 1000);
for (int i = 0; i < 20; ++i)
arr[i] += h.getOne();
}
for (int i = 0; i < 20; i++)
System.out.println(arr[i]);
}我测试的结果
198741
198305
197537
195746
196592
198304
202119
198655
200099
200290
198616
205446
198713
200420
203392
199065
205758
200732
198460
203010
相关文章推荐
- git 常用命令
- 二维数组应用:迷宫游戏
- string StartsWith 方法 Https
- Hazelcast入门教程
- asp 上传文件包Microsoft VBScript 运行时错误 '800a0005'无效的过程调用或参数: 'Instr'
- zabbix使用自带模板监控mysql
- Eclipse + Axis2 开发 WebService
- 11. Magento路由分发过程解析(一):在前端控制器中获取路由对象
- ps从0学习
- .Net学习笔记----2015-06-24(ArrayList集合练习)
- 剑指offer-第四章解决面试题的思路(栈的压入和弹出序列)
- 备战2015GRE之核心词组on grounds of
- SAT写作备考经典例子之古腾堡
- Android Touch事件传递机制解析
- 频率混叠现象
- Mysql中的循环语句
- 解决tableView上cell的按钮点击效果不明显(轻点无效)
- IOS 轻量级视频播放器
- 2--嵌入式操作系统FreeRTOS的原理与实现
- arm下蜂鸣器测试代码