2025-05-23

Java代碼 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */ 
  
package test; 
import java.math.BigInteger; 
import java.math.BigDecimal; 
/**
 * 2011-3-19
 * @author Jasper
 */ 
public class RedandBlue { 
    public static void main(String[] args) { 
        System.out.println(“雙色球中獎概率(6/33+1/16):”); 
        BigInteger allOdds = getOdds(33,6).multiply(getOdds(16,1)); 
        BigInteger odds1 = getOdds(6,6).multiply(getOdds(1,1)); 
        BigInteger odds2 = getOdds(6,6).multiply(getOdds(15,1)); 
        BigInteger odds3 = getOdds(6,5).multiply(getOdds(27,1)).multiply(getOdds(1,1)); 
        BigInteger odds4 = getOdds(6,5).multiply(getOdds(27,1)).multiply(getOdds(15,1)).add(getOdds(6,4).multiply(getOdds(27,2)).multiply(getOdds(1,1))); 
        BigInteger odds5 = getOdds(6,4).multiply(getOdds(27,2)).multiply(getOdds(15,1)).add(getOdds(6,3).multiply(getOdds(27,3)).multiply(getOdds(1,1))); 
        BigInteger odds6 = getOdds(6,2).multiply(getOdds(27,4)).multiply(getOdds(1,1)).add(getOdds(6,1).multiply(getOdds(27,5)).multiply(getOdds(1,1))).add(getOdds(27,6).multiply(getOdds(1,1))); 
        BigInteger odds1_7 = odds1.add(odds2).add(odds3).add(odds4).add(odds5).add(odds6); 
        BigInteger odds7 = allOdds.add(odds1_7.negate()); 
        String odds11 = BigDecimal.valueOf(odds1.longValue()).pide(BigDecimal.valueOf(allOdds.longValue()),8,BigDecimal.ROUND_HALF_EVEN).toPlainString(); 
        String odds21 = BigDecimal.valueOf(odds2.longValue()).pide(BigDecimal.valueOf(allOdds.longValue()),8,BigDecimal.ROUND_HALF_EVEN).toPlainString(); 
        String odds31 = BigDecimal.valueOf(odds3.longValue()).pide(BigDecimal.valueOf(allOdds.longValue()),8,BigDecimal.ROUND_HALF_EVEN).toPlainString(); 
        String odds41 = BigDecimal.valueOf(odds4.longValue()).pide(BigDecimal.valueOf(allOdds.longValue()),4,BigDecimal.ROUND_HALF_EVEN).toPlainString(); 
        String odds51 = BigDecimal.valueOf(odds5.longValue()).pide(BigDecimal.valueOf(allOdds.longValue()),4,BigDecimal.ROUND_HALF_EVEN).toPlainString(); 
        String odds61 = BigDecimal.valueOf(odds6.longValue()).pide(BigDecimal.valueOf(allOdds.longValue()),4,BigDecimal.ROUND_HALF_EVEN).toPlainString(); 
        String odds71 = BigDecimal.valueOf(odds7.longValue()).pide(BigDecimal.valueOf(allOdds.longValue()),4,BigDecimal.ROUND_HALF_EVEN).toPlainString(); 
  
        double check=(new Double(odds11))+(new Double(odds21))+(new Double(odds31))+(new Double(odds41))+(new Double(odds51))+(new Double(odds61))+(new Double(odds71)); 
        System.out.println(“一等獎(中6+1)中獎概率:”+odds1+”/”+allOdds+”=”+odds11); 
        System.out.println(“二等獎(中6+0):”+odds2+”/”+allOdds+”=”+odds21); 
        System.out.println(“三等獎(中5+1)(3000元)中獎概率:”+odds3+”/”+allOdds+”=”+odds31); 
        System.out.println(“四等獎(中5+0或4+1)(200元)中獎概率:”+odds4+”/”+allOdds+”=”+odds41); 
        System.out.println(“五等獎(中4+0或3+1)(10元)中獎概率:”+odds5+”/”+allOdds+”=”+odds51); 
        System.out.println(“六等獎(中2+1或1+1或0+1)(5元)中獎概率:”+odds6+”/”+allOdds+”=”+odds61); 
        System.out.println(“不中獎概率:”+odds7+”/”+allOdds+”=”+odds71); 
        System.out.println(“驗證結果,概率相加之和為”+check); 
    } 
  
    private static BigInteger getOdds(Integer n,Integer k) { 
        BigInteger lotteryOdds = BigInteger.valueOf(1); 
        for(int i = 1; i<=k;i++) { 
            lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n-i+1)).pide(BigInteger.valueOf(i)); 
        } 
        return lotteryOdds; 
    } 

 結果:
run:
雙色球中獎概率(6/33+1/16):
一等獎(中6+1)中獎概率:1/17721088=0.00000006
二等獎(中6+0):15/17721088=0.00000085
三等獎(中5+1)(3000元)中獎概率:162/17721088=0.00000914
四等獎(中5+0或4+1)(200元)中獎概率:7695/17721088=0.0004
五等獎(中4+0或3+1)(10元)中獎概率:137475/17721088=0.0078
六等獎(中2+1或1+1或0+1)(5元)中獎概率:1043640/17721088=0.0589
不中獎概率:16532100/17721088=0.9329
驗證結果,概率相加之和為1.00001005
成功生成(總時間:0 秒)
 
祝大傢中大獎,交好運!
<!– –>

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *