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
| package demo.game;
import cn.hutool.core.util.RandomUtil; import com.alibaba.fastjson.JSON;
import java.util.*;
public class DoubleColorBallLottery {
private int lotteryNumber = 0;
private Set<Integer> winRedNum = new HashSet<>(); private int winBlueNum;
private final List<List<Integer>> userLottery = new ArrayList<>();
private final Map<Integer, List<String>> userPrizeLottery = new HashMap<>();
public int[] lotteryAmount = new int[]{0, 5000000, 1250000, 3000, 200, 10, 5};
private static final int MAX_PRIZE_LEVEL = 6;
Random random = new Random();
public void lotteryDrawing() { winRedNum = buildRedNum(); winBlueNum = buildBlueNum(); System.out.printf("[第%s期双色球]开奖结果:红球:%s,蓝球:%s%n", lotteryNumber, JSON.toJSONString(winRedNum), winBlueNum); }
private int buildBlueNum() { return random.nextInt(16) + 1; }
private HashSet<Integer> buildRedNum() { final HashSet<Integer> set = new HashSet<>(); while (set.size() < 6) { final int randomNum = random.nextInt(33) + 1; set.add(randomNum); } return set; }
public void buy(int count) { lotteryNumber = RandomUtil.randomInt(999); for (int i = 0; i < count; i++) { final ArrayList<Integer> lottery = new ArrayList<>(buildRedNum()); lottery.add(buildBlueNum()); userLottery.add(lottery); } }
public void redemption() {
for (int i = 1; i <= MAX_PRIZE_LEVEL; i++) { userPrizeLottery.put(i, new ArrayList<>()); } for (List<Integer> lottery : userLottery) { int readBallMatches = countMatchingNumbers(lottery); boolean blueBallMatch = lottery.get(6) == winBlueNum; int prizeItem = calculatePrize(readBallMatches, blueBallMatch); if (prizeItem != 0) {
final List<String> numbers = userPrizeLottery.get(prizeItem); numbers.add(JSON.toJSONString(lottery)); } } System.out.println(String.format("[第%s期双色球]中奖结果:", lotteryNumber)); int userPrizeCount = 0; int userPrizeAmount = 0; for (int i = 1; i <= MAX_PRIZE_LEVEL; i++) { final int userPrizeItemCount = userPrizeLottery.get(i).size(); userPrizeCount += userPrizeItemCount; userPrizeAmount += lotteryAmount[i] * userPrizeItemCount; if (i <= 3) { System.out.println(String.format("%s等奖中奖个数:%s,中奖号码:%s", i, userPrizeItemCount, userPrizeLottery.get(i))); } else { System.out.println(String.format("%s等奖中奖个数:%s,中奖号码:-", i, userPrizeItemCount)); } }
System.out.println(String.format("一共买了[%s张]彩票,中奖个数=%s,中奖概率=%s;购买彩票费用=%s,奖金=%s,盈利=%s" , userLottery.size(), userPrizeCount, 1.0 * userPrizeCount / userLottery.size(), userLottery.size() * 2, userPrizeAmount, userPrizeAmount - userLottery.size() * 2)); }
private int calculatePrize(int readBallMatches, boolean blueBallMatch) { if (readBallMatches == 6 && blueBallMatch) { return 1; } else if (readBallMatches == 6) { return 2; } else if (readBallMatches == 5 && blueBallMatch) { return 3; } else if (readBallMatches == 5 || (readBallMatches == 4 && blueBallMatch)) { return 4; } else if (readBallMatches == 4 || (readBallMatches == 3 && blueBallMatch)) { return 5; } else if (blueBallMatch) { return 6; } return 0; }
private int countMatchingNumbers(List<Integer> lottery) { int count = 0; for (int i = 0; i < 6; i++) { if (winRedNum.contains(lottery.get(i))) { count += 1; } } return count; }
public void game(int n) { buy(n); lotteryDrawing(); redemption(); }
public static void main(String[] args) { final DoubleColorBallLottery lottery = new DoubleColorBallLottery(); lottery.game(100000); }
}
|