码迷,mamicode.com
首页 > 编程语言 > 详细

java笔试

时间:2020-07-24 09:26:37      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:public   span   break   first   array   bool   length   alt   view   

技术图片
  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 }
View Code
技术图片
 1 package head.first.java.prize;
 2 
 3 public class Prize {
 4     private Integer prizeId;//奖品id
 5     private Double probability;//中奖概率
 6     private Integer quantity;//奖品数量
 7 
 8     public Integer getPrizeId() {
 9         return prizeId;
10     }
11 
12     public void setPrizeId(Integer prizeId) {
13         this.prizeId = prizeId;
14     }
15 
16     public Double getProbability() {
17         return probability;
18     }
19 
20     public void setProbability(Double probability) {
21         this.probability = probability;
22     }
23 
24     public Integer getQuantity() {
25         return quantity;
26     }
27 
28     public void setQuantity(Integer quantity) {
29         this.quantity = quantity;
30     }
31 }
View Code

 

java笔试

标签:public   span   break   first   array   bool   length   alt   view   

原文地址:https://www.cnblogs.com/xblovexb/p/13369690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!