跳至主要内容

乒乓球

假设单个球的胜率为p,计算一整局的胜率。
直接算过于麻烦,遂直接蒙特卡洛。
————————————————————————————————————————
#include <iostream>
using namespace std;

bool w(int x, int y){
    if (x == 11){
        if (y < 10){
            return true;
        }
    }if (x > 11){
        if (x - y > 1){
            return true;
        }
    }
    return false;
}

bool l(int y, int x){
    if (x == 11){
        if (y < 10){
            return true;
        }
    }if (x > 11){
        if (x - y > 1){
            return true;
        }
    }
    return false;
}

int main() {
    srand((unsigned int)time(0));
    int x, y;
    int s;
    for (int p = 0; p < 101; ++p) {


        s = 0;
        for (int i = 0; i < 1000000; ++i) {
            x = 0;
            y = 0;
            while (!w(x, y) && !l(x, y)) {
                if (random() % 100 < p) {
                    x++;
                } else {
                    y++;
                }
            }
            if (w(x, y)) {
                s++;
            }

        }
        cout << p / 100.0 << ',' << s / 1000000.0 << endl;
    }
    return 0;
}
———————————————————————————————————————————————————————
结果:
0,0
0.01,0
0.02,0
0.03,0
0.04,0
0.05,0
0.06,0
0.07,0
0.08,1e-06
0.09,0
0.1,0
0.11,2e-06
0.12,3e-06
0.13,1.3e-05
0.14,2.2e-05
0.15,4.8e-05
0.16,7.2e-05
0.17,0.000144
0.18,0.000258
0.19,0.000398
0.2,0.000719
0.21,0.001036
0.22,0.001573
0.23,0.002329
0.24,0.00352
0.25,0.005047
0.26,0.006972
0.27,0.009365
0.28,0.012648
0.29,0.016782
0.3,0.021624
0.31,0.028365
0.32,0.036035
0.33,0.044903
0.34,0.05598
0.35,0.068796
0.36,0.08342
0.37,0.100263
0.38,0.118615
0.39,0.140052
0.4,0.16327
0.41,0.19014
0.42,0.217076
0.43,0.247343
0.44,0.279067
0.45,0.313513
0.46,0.348111
0.47,0.385644
0.48,0.422838
0.49,0.461707
0.5,0.499091
0.51,0.537828
0.52,0.576643
0.53,0.614962
0.54,0.651756
0.55,0.686844
0.56,0.720364
0.57,0.752608
0.58,0.783135
0.59,0.810359
0.6,0.836101
0.61,0.859253
0.62,0.881507
0.63,0.899735
0.64,0.916659
0.65,0.931601
0.66,0.943854
0.67,0.955077
0.68,0.963709
0.69,0.971564
0.7,0.978114
0.71,0.983224
0.72,0.987493
0.73,0.990643
0.74,0.993047
0.75,0.995067
0.76,0.996511
0.77,0.997593
0.78,0.99844
0.79,0.998948
0.8,0.999279
0.81,0.999563
0.82,0.99973
0.83,0.999851
0.84,0.999927
0.85,0.999967
0.86,0.999977
0.87,0.999987
0.88,0.999994
0.89,0.999998
0.9,1
0.91,1
0.92,1
0.93,1
0.94,1
0.95,1
0.96,1
0.97,1
0.98,1
0.99,1
1,1

Process finished with exit code 0
———————————————————————————————————————————————————————
————————————————————————————————————————
若干二项分布的累积,大概......近似正态分布?(图比较像......)待考虑。

评论