/** * 计算C(n,m)组合数的值 */ publiclongcombination(int n, int m){ long[] arr = newlong[m + 1]; for (int i = 0; i < arr.length; i++) { arr[i] = 1; } for (int i = 1; i <= n - m; i++) { for (int j = 1; j <= m; j++) { arr[j] += arr[j - 1]; } } return arr[m]; }
publicstaticvoidmain(String[] args){ BernoulliExperiment bernoulli = new BernoulliExperiment(); double p = 0.05; int n = 50; double oddProbability = 0, evenProbability = 0; for (int i = 0; i <= n; i++) { double probability = bernoulli.probability(n, i, p); if (i % 2 == 0) { evenProbability += probability; } else { oddProbability += probability; } } System.out.println("Odd probability : " + oddProbability); System.out.println("Even probability : " + evenProbability); System.out.println("Odd + Even : " + (evenProbability + oddProbability)); } }