-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week7.java
404 lines (370 loc) · 13 KB
/
Week7.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package week_7.wuyanzhen;
import util.wuyanzhen.FlorenceQueue;
import util.wuyanzhen.FlorenceStack;
import util.wuyanzhen.TreeNode;
import java.util.*;
/**
* @author Florence
*/
public class Week7 {
private static ArrayList<Double> poissonDistribute = new ArrayList<Double>();
private static ArrayList<Double> exponentDistribute = new ArrayList<Double>();
private static int MAX_POSSIBLE = 1000;
private static final boolean WASTE_ZOOM_WAY = true;
private static final boolean HARD_GET_SON_WAY = false;
static {
for (int i = 0; i < MAX_POSSIBLE; i++) {
poissonDistribute.add(getPoissonNum(i));
exponentDistribute.add(getExponent(i));
}
}
static class Consumer {
String consumerName;
long remindTime = 0;
long stayTime;
Consumer(long stay, String name) {
remindTime = stay;
consumerName = name;
stayTime = stay;
}
public long getRemindTime() {
return remindTime;
}
public void setRemindTime(long remindTime) {
this.remindTime = remindTime;
}
public String getName() {
return consumerName;
}
public void setName(String name) {
this.consumerName = name;
}
@Override
public String toString() {
return consumerName + ":服务时长" + stayTime + "小时";
}
}
static class TreeNodeLessZoom<T> {
T data;
int parent = 0;
TreeNodeLessZoom(int initParent) {
parent = initParent;
}
}
public static void main(String[] args) throws Exception {
// cashierDeskEmulation();
// Map<String, String> map = recognizeTheCLanguageCode("#include<stdio.h>\n" +
// "int main()\n" +
// "{\n" +
// " int a,b;\n" +
// " int c;\n" +
// " c=a+b;\n" +
// " if (a>0)\n" +
// " {\n" +
// " }\n" +
// " else\n" +
// " {\n" +
// " }\n" +
// " return 0;\n" +
// "}\n" +
// "\n");
// for (Map.Entry<String,String> entry:map.entrySet()){
// System.out.println(entry.getKey()+":"+entry.getValue());
// }
// TreeNodeLessZoom<Object>[] treeNodeLessZoomArr = getTreeNodeArr(10);
// List<Integer> sonNodeByClass = getSonNodeByClass(treeNodeLessZoomArr[1], treeNodeLessZoomArr);
// System.out.println(sonNodeByClass);
// System.out.println(Arrays.toString(buildBinaryTree(new Integer[]{2,3,4,5,7,9,10,6,8,11})));
// List<Integer> list = new LinkedList<>();
// for (int i=0;i<10;i++){
// list.add(1);
// list.add(2);
// list.remove(0);
// list.remove(1);
// }
showTree(buildBinaryTree(new Integer[]{0,1,2,3,4,5,6,7,8}));
}
/**
* 将乱序利用两个栈回复成顺序
*
* @param disOrderStr 乱序字符串
* @return 恢复完的字符串
* @throws Exception 字符串异常
*/
public static String recoverOrder(String disOrderStr) throws Exception {
if (disOrderStr.trim().isEmpty()) {
throw new Exception("参数异常");
}
char[] toCharArray = disOrderStr.toCharArray();
FlorenceStack<Character> ascStack = new FlorenceStack<Character>();
FlorenceStack<Character> descStack = new FlorenceStack<Character>();
for (char c : toCharArray) {
//一开始或者符合顺序直接入栈
if (ascStack.size() == 0 || c < ascStack.top()) {
ascStack.push(c);
} else {
//找到那个字符应在在的位置然后入栈
while (!ascStack.isEmpty() && c > ascStack.top()) {
descStack.push(ascStack.pop());
}
ascStack.push(c);
//将那些之前出栈的元素入栈
while (!descStack.isEmpty()) {
ascStack.push(descStack.pop());
}
}
}
return ascStack.toString();
}
/**
* 收银台仿真
*
* @throws Exception
*/
public static void cashierDeskEmulation() throws Exception {
FlorenceQueue<Consumer> queue = new FlorenceQueue<Consumer>();
long consumerNumber = 1;
do {
//泊松分布生成概率*当前的x(也就是单位时间来x人乘于泊松分布的概率当作单位时间来的人)
long poissonNum = getNewConsumerCount();
consumerNumber = addNewConsumer(queue, poissonNum, consumerNumber);
if (!queue.isEmpty()) {
//获取正在服务的人
Consumer front = queue.front();
//如果服务时间已经为0,出队列;
if (front.getRemindTime() == 0) {
System.out.println(queue.deQueue());
}
front.setRemindTime(front.getRemindTime() - 1);
}
} while (!queue.isEmpty() && queue.size() <= 200);
}
public static Map<String, String> recognizeTheCLanguageCode(String CLanguageCode) {
Map<String, String> resMap = new HashMap<String, String>(20);
List<String[]> list = new ArrayList<String[]>();
for (String str : CLanguageCode.split("\n")) {
list.add(str.trim().split(" "));
}
List<String> cDataTypeKeyWordList = Arrays.asList("char", "int", "float", "double", "short", "long", "signed", "unsigned");
List<String> cOperateKeyWordList = Arrays.asList("return", "continue", "break", "goto", "if", "else", "switch", "case", "default", "for", "do", "while");
for (String[] strings : list) {
String temp = strings[0];
for (String s : cDataTypeKeyWordList) {
if (temp.equals(s)) {
for (int i = 1; i < strings.length; i++) {
for (String str : strings[i].split(",")) {
if (!" ".equals(str)) {
resMap.put(str.replace(";", ""), "变量名");
}
}
}
break;
}
}
for (String s : strings) {
for (String operate : cOperateKeyWordList) {
if (s.contains(operate)) {
resMap.put(operate, "操作符");
}
}
}
}
return resMap;
}
public static int getParentNodeByArrWay(int curNodeIndex) {
return curNodeIndex / 2;
}
public static int getSonNodeByArrWay(int curNodeIndex, String leftOrRight) {
int gap= "left".equals(leftOrRight)?0:1;
return curNodeIndex * 2+1;
}
public static <T> int getParentNodeByClass(TreeNodeLessZoom<T> node){
return node.parent;
}
/**
* 用节省空间的方式来存储树,然后要获取他的子节点
* @param node
* @param treeNodeLessZooms
* @param <T>
* @return
*/
public static <T> List<Integer> getSonNodeByClass(TreeNodeLessZoom<T> node, TreeNodeLessZoom[] treeNodeLessZooms){
int selfIndex = -1;
List<Integer> list = new LinkedList<Integer>();
for (int i = 0; i< treeNodeLessZooms.length; i++){
if (node== treeNodeLessZooms[i]){
selfIndex=i;
break;
}
}
for (int i = 1; i< treeNodeLessZooms.length; i++){
if (selfIndex== treeNodeLessZooms[i].parent){
list.add(i);
}
}
return list;
}
public static <T> TreeNode<T>[] buildBinaryTree(T[] wantToBuildArr) throws Exception {
LinkedList<TreeNode<T>> list = new LinkedList<TreeNode<T>>();
if (wantToBuildArr==null){
System.out.println("Arr is null");
return null;
}
if (wantToBuildArr.length==0){
System.out.println("length is 0");
return null;
}
FlorenceQueue<T> florenceQueue = new FlorenceQueue<T>();
florenceQueue.enQueue(wantToBuildArr[0]);
int count=1;
while(!florenceQueue.isEmpty()){
//根据循环添加某一层的元素,记得最后一层可能不是满的
for (int i=0;i<count&&!florenceQueue.isEmpty();i++){
list.add(new TreeNode<T>(florenceQueue.deQueue()));
}
for (int i=count;i<count*2&&i<wantToBuildArr.length;i++){
florenceQueue.enQueue(wantToBuildArr[i]);
}
count*=2;
}
return getResultBinaryTreeArr(list);
}
private static <T> TreeNode<T>[] getResultBinaryTreeArr(LinkedList<TreeNode<T>> list) {
TreeNode<T>[] treeNodes = new TreeNode[list.size()+1];
for (int i=1;i<treeNodes.length;i++){
treeNodes[i]=list.get(i-1);
}
return treeNodes;
}
public static <T> void showTree(TreeNode<T>[] treeNodes){
int length=treeNodes.length-1;
int rowCount= (int) (isPowerOfTwo(length)?log2(length):Math.ceil(log2(length)));
int bottomLength= (int) Math.pow(2,rowCount-1);
FlorenceQueue<TreeNode<T>> queue = new FlorenceQueue<TreeNode<T>>();
queue.enQueue(treeNodes[1]);
int count=1;
while (!queue.isEmpty()){
String blankAndData= getBlank(bottomLength/2-((count+count-1)/2));
for (int i=0;i<count&&!queue.isEmpty();i++){
blankAndData+=queue.deQueue().getData()+" ";
int index = (count + i) * 2;
//左节点入队列
if (index <=length){
queue.enQueue(treeNodes[index]);
}
//右节点入队列
if (index+1<=length){
queue.enQueue(treeNodes[index+1]);
}
}
//输出这一层的内容
System.out.println(blankAndData);
count*=2;
}
}
private static String getBlank(int length) {
StringBuilder stringBuilder = new StringBuilder();
for (int i=0;i<length;i++){
stringBuilder.append(" ");
}
return stringBuilder.toString();
}
public static double log2(double N) {
//Math.log的底为e
return Math.log(N)/Math.log(2);
}
/**
* 根据泊松分布给的个数进行人数的添加
*
* @param queue 收银台队列
* @param poisson 泊松分布的
* @param y
* @return
*/
private static long addNewConsumer(FlorenceQueue<Consumer> queue, long poisson, long y) {
for (int i = 1; i <= poisson; i++) {
queue.enQueue(new Consumer(getConsumerStayTime(), (y + i) + "号顾客"));
}
return y + poisson;
}
public static boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
/**
* 根据泊松分布获取新来的消费者的数量
*
* @return
*/
private static long getNewConsumerCount() {
double random = Math.random();
double sum = 0;
for (int i = 0; i < poissonDistribute.size(); i++) {
Double temp = poissonDistribute.get(i);
if (random >= sum && random <= sum + temp) {
return i;
}
sum += temp;
}
//一定要在前面的概率取到
return -666666666;
}
/**
* 根据指数分布获取的新消费者待的时间
*
* @return
*/
private static int getConsumerStayTime() {
double random = Math.random();
double sum = 0;
for (int i = 0; i < exponentDistribute.size(); i++) {
Double temp = poissonDistribute.get(i);
if (random > sum && random < sum + temp) {
return i;
}
sum += temp;
}
//一定要在前面的概率取到
return -666666666;
}
/**
* 获取泊松分布函数
*
* @param k
* @return
*/
public static double getPoissonNum(long k) {
return Math.pow(3, k) / getFactorial(k) * Math.pow(Math.E, -3);
}
/**
* 指数分布
*
* @param k 参数k
* @return
*/
public static double getExponent(long k) {
if (k <= 0) {
return 0;
}
return (2 * Math.pow(Math.E, -2 * k));
}
/**
* 获取阶乘
*
* @param n 要获取的阶乘数目
* @return 得到的阶乘数
*/
public static double getFactorial(long n) {
double sum = 1;
for (int i = 2; i <= n; i++) {
sum *= i;
}
return sum;
}
private static <T> TreeNodeLessZoom<T>[] getTreeNodeArr(int length){
TreeNodeLessZoom<T>[] treeNodeLessZooms =new TreeNodeLessZoom[length+1];
for (int i=1;i<=length;i++){
treeNodeLessZooms[i]=new TreeNodeLessZoom<T>(i/2);
}
return treeNodeLessZooms;
}
}