1 package head.first.java.prize;
2
3 import java.util.*;
4
5 public class test {
6 private static final int mulriple = 5000;
7 public int luckDraw(List<Prize> prizes) {
8 int lastScope = 0;
9 Collections.shuffle(prizes);
10 Map<Integer, int[]> prizeScopes = new HashMap<Integer, int[]>();//区间
11 Map<Integer, Integer> prizeQuantity = new HashMap<Integer, Integer>();//数量
12 for (Prize prize : prizes) {
13 int prizeId = prize.getPrizeId();
14 int currentScope = lastScope + (int)(prize.getProbability()*mulriple);//区间划分
15 prizeScopes.put(prizeId, new int[] { lastScope + 1, currentScope });//奖品中奖的区间
16 prizeQuantity.put(prizeId, prize.getQuantity());//有多少奖品
17 lastScope = currentScope;
18 }
19 int luckyNumber = new Random().nextInt(mulriple);
20 int luckyPrizeId = 0;
21 if ((null != prizeScopes) && !prizeScopes.isEmpty()) {
22 Set<Map.Entry<Integer, int[]>> entrySets = prizeScopes.entrySet();
23 for (Map.Entry<Integer, int[]> m : entrySets) {
24 int key = m.getKey();
25 if (luckyNumber >= m.getValue()[0] && luckyNumber <= m.getValue()[1] && prizeQuantity.get(key) > 0) {
26 luckyPrizeId = key;
27 break;
28 }
29 }
30 }
31 if (luckyPrizeId > 0) {
32 int i = prizeQuantity.get(luckyPrizeId);
33 i--;
34 prizeQuantity.put(luckyPrizeId,i);
35 }
36 return luckyPrizeId;
37 }
38 public void findNumber() {
39 String[] array={"1","5","33","7","8","27","5","90","2","6","25"};
40 int[] arr = new int[array.length];
41 int max = 0;
42 int min = 0;
43 for(int x=0; x<arr.length; x++){
44 arr[x]=Integer.parseInt(array[x]);
45 if(arr[x]>arr[max]){
46 max = x;
47 }
48 if(arr[x]<arr[min]){
49 min = x;
50 }
51 }
52 System.out.println(arr[max]*arr[min]); ;
53 }
54 public static boolean checkEqual(Integer num) {
55 return num==100;
56 }
57 public Map<Integer,Integer> findAndReturn(List<Integer> list){
58 Map<Integer,Integer> map = new HashMap<Integer,Integer>();
59 int i=0,j=0,z=0;
60 for(;i<list.size();i++){
61 char lastChar = String.valueOf(list.get(i)).charAt(String.valueOf(list.get(i)).length()-1);
62 if((lastChar=='5'||lastChar=='0')&&!checkEqual(list.get(i))){
63 j++;
64 }
65 if((lastChar=='0'||lastChar=='2'||lastChar=='4'||lastChar=='6'||lastChar=='8')&&!checkEqual(list.get(i))){
66 z++;
67 }
68 }
69 map.put(2,z);
70 map.put(5,j);
71 return map;
72 }
73 public static void main(String[] args) {
74 List<Prize> prizes = new ArrayList<Prize>();
75 Prize prize1 = new Prize();
76 prize1.setPrizeId(1);//id
77 prize1.setProbability(0.0006);//概率3/5000
78 prize1.setQuantity(3);//数量
79 prizes.add(prize1);
80 Prize prize2 = new Prize();
81 prize2.setPrizeId(2);//id
82 prize2.setProbability(0.02);//概率100/5000
83 prize2.setQuantity(100);//数量
84 prizes.add(prize2);
85 Prize prize3 = new Prize();
86 prize3.setPrizeId(3);//id
87 prize3.setProbability(0.01);//概率50/5000
88 prize3.setQuantity(50);//数量
89 prizes.add(prize3);
90 int prizeOne = 0;
91 int prizeTwo = 0;
92 int prizeThree = 0;
93 test test =new test();
94 for (int i = 0; i < 1000; i++) {
95 int prizeId = test.luckDraw(prizes);
96 switch (prizeId) {
97 case 1:
98 prizeOne++;
99 break;
100 case 2:
101 prizeTwo++;
102 break;
103 case 3:
104 prizeThree++;
105 break;
106 }
107 }
108 System.out.println("prize1中奖次数" + prizeOne);
109 System.out.println("prize2中奖次数" + prizeTwo);
110 System.out.println("prize3中奖次数" + prizeThree);
111 test.findNumber();
112 List<Integer> list = new ArrayList<Integer>();
113 list.add(1);
114 list.add(2);
115 list.add(5);
116 list.add(10);
117 list.add(20);
118 list.add(25);
119 list.add(27);
120 list.add(100);
121 list.add(99);
122 System.out.println(test.findAndReturn(list).toString());
123 }
124 }