kiinlam
3/25/2016 - 4:19 AM

双色球中奖注数计算

双色球中奖注数计算

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <fieldset>
        <legend>普通</legend>
        <p>红<input id="red" type="text" /></p>
        <p>蓝<input id="blue" type="text" /></p>
        <p>红命中<input id="mred" type="text" /></p>
        <p>蓝命中<input id="mblue" type="text" /></p>
        <button onclick="calculate()">计算</button>
    </fieldset>
    <fieldset>
        <legend>胆拖</legend>
        <p>胆红<input id="dred" type="text" /></p>
    	<p>拖红<input id="tred" type="text" /></p>
        <p>蓝<input id="blues" type="text" /></p>
        <p>胆红命中<input id="mdred" type="text" /></p>
    	<p>拖红命中<input id="mtred" type="text" /></p>
    	<p>蓝命中<input id="mblues" type="text" /></p>
    	<button onclick="dtcalculate()">计算</button>
    </fieldset>
    <div>结果在控制台查看</div>
<script>
    var qs = function () {
        return document.querySelector.apply(document, arguments);
    };
	var calculate = function() {
        var red = qs("#red").value * 1
          , blue = qs("#blue").value * 1
          , mred = qs("#mred").value * 1
          , mblue = qs("#mblue").value * 1
          , awards = [0, 0, 0, 0, 0, 0];
        switch (mred) {
        case 0:
        case 1:
        case 2:
            mblue > 0 && (awards[5] += combination(red, 6));
            break;
        case 3:
        	if (mblue > 0) {
        		awards[5] += combination(red - mred, 4) * combination(mred, 2);
	            awards[5] += combination(red - mred, 5) * combination(mred, 1);
	            awards[5] += combination(red - mred, 6);
	            awards[4] += combination(red - mred, 3);
        	}
            break;
        case 4:
        	if (mblue > 0) {
        		awards[5] += combination(red - mred, 6);
	            awards[5] += combination(red - mred, 5) * combination(mred, 1);
	            awards[5] += combination(red - mred, 4) * combination(mred, 2);
	            awards[4] += combination(red - mred, 3) * combination(mred, 3);
	            awards[4] += combination(red - mred, 2) * combination(mred, 4) * (blue - 1);
	            awards[3] += combination(red - mred, 2);
        	} else {
        		awards[4] += combination(red - mred, 2) * blue;
        	}
            break;
        case 5:
        	if (mblue > 0) {
        		awards[5] += combination(red - mred, 6);
	            awards[5] += combination(red - mred, 5) * combination(mred, 1);
	            awards[5] += combination(red - mred, 4) * combination(mred, 2);
	            awards[4] += combination(red - mred, 3) * combination(mred, 3);
	            awards[4] += combination(red - mred, 2) * combination(mred, 4) * (blue - 1);
	            awards[3] += combination(red - mred, 2) * combination(mred, 4);
	            awards[3] += combination(red - mred, 1) * combination(mred, 5) * (blue - 1);
	            awards[2] += combination(red - mred, 1);
        	} else {
        		awards[4] += combination(red - mred, 2) * combination(mred, 4) * blue;
	            awards[3] += combination(red - mred, 1) * combination(mred, 5) * blue;
        	}
            break;
        case 6:
        	if (mblue > 0) {
        		awards[5] += combination(red - mred, 6);
	            awards[5] += combination(red - mred, 5) * combination(mred, 1);
	            awards[5] += combination(red - mred, 4) * combination(mred, 2);
	            awards[4] += combination(red - mred, 3) * combination(mred, 3);
	            awards[4] += combination(red - mred, 2) * combination(mred, 4) * (blue - 1);
	            awards[3] += combination(red - mred, 2) * combination(mred, 4);
	            awards[3] += combination(red - mred, 1) * combination(mred, 5) * (blue - 1);
	            awards[2] += combination(red - mred, 1) * combination(mred, 5);
	            awards[1] += blue - 1;
	            awards[0]++;
        	} else {
        		awards[4] += combination(red - mred, 2) * combination(mred, 4) * blue;
	            awards[3] += combination(red - mred, 1) * combination(mred, 5) * blue;
	            awards[1] += blue;
        	}
        	break;
        }
        show(awards);
    };
    var dtcalculate = function() {
        var dred = qs("#dred").value * 1
          , tred = qs("#tred").value * 1
          , blues = qs("#blues").value * 1
          , mdred = qs("#mdred").value * 1
          , mtred = qs("#mtred").value * 1
          , mblues = qs("#mblues").value * 1;
        if (dred + tred < 6) {
            return;
        }
        var awards = [0, 0, 0, 0, 0, 0];
        awards = dtcombination(dred, tred, blues, mdred, mtred, mblues);
        show(awards);
    };
    var combination = function(a, b) {
        var n, r;
        a / 2 < b && (b = a - b);
        if (a < b || b < 0) {
            return 0;
        }
        if (a >= 0 && b === 0) {
            return 1;
        }
        n = 1;
        r = a;
        for (var i = 1; i <= b; i++) {
            n *= i;
            i < b && (r *= a - i);
        }
        return r / n;
    };
    var dtcombination = function(a, b, c, x, y, z) {
        var y2 = y <= 6 - a ? y : 6 - a
          , mz = [];
        for (var i = 0; i <= y2; i++) {
            mz[i] = x + i;
        }
        var result = [0, 0, 0, 0, 0, 0]
          , m = 0;
        if (z == 1) {
            for (var i = 0; i <= y2; i++) {
                m = rank(mz[i], 0);
                m != -1 && (result[m] += C(b - y, 6 - a - i) * (c - 1) * C(y, i));
                m = rank(mz[i], 1);
                result[m] += C(b - y, 6 - a - i) * C(y, i);
            }
        }
        if (z == 0){
            for (var i = 0; i <= y2; i++) {
                m = rank(mz[i], 0);
                m != -1 && (result[m] += C(b - y, 6 - a - i) * c * C(y, i));
            }
        }
        return result;
    };
    var rank = function(e, t) {
        var n = e * 1 + t * 1;
        switch (n) {
        case 7:
            return 0;
        case 6:
            return t == 1 ? 2 : 1;
        case 5:
            return 3;
        case 4:
            return 4;
        default:
            return t == 1 ? 5 : -1
        }
    };
    var C = function(e, n) {
        if (n <= e) {
            var r = F(e - n) * F(n);
            r = F(e) / r;
            return r;
        }
        return 0;
    };
    var F = function(e) {
        return e == 0 ? 1 : F(e - 1) * e;
    };
    var show = function(arr) {
        console.log('中奖注数:');
    	console.log(arr);
    };
</script>
</body>
</html>