日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

抛硬币正面期望_如果抛硬币,正面的数量多于反面的可能性

發(fā)布時(shí)間:2023/12/1 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 抛硬币正面期望_如果抛硬币,正面的数量多于反面的可能性 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

拋硬幣正面期望

Problem statement:

問題陳述:

Let N be a positive odd number. There are N coins, numbered 1, 2 , ... , N. For each i (1≤i≤N), when Coin i is tossed, it comes up heads with probability pi and tails with probability 1-pi.

N為正奇數(shù)。 有N個(gè)硬幣,編號(hào)為1,2,...,N 。 對于每個(gè)i ( 1≤i≤N ),當(dāng)投入硬幣i時(shí),它的出現(xiàn)概率為pi出現(xiàn)在正面 ,出現(xiàn)的概率為1-pi出現(xiàn)在尾部。

Find the probability of having more heads than tails if coins are tossed.

找到拋硬幣后正面多于尾的概率。

Input:

輸入:

The first line contains N, number of coins. The second line contains the probability of coming head in the coins.

第一行包含N個(gè)硬幣數(shù)量。 第二行包含進(jìn)入硬幣的概率。

Output:

輸出:

Output the probability of having more number of heads than tails if coins are tossed.

如果拋硬幣,則輸出正面比背面多的概率。

Example with explanation:

帶有說明的示例:

Input: N=3[0.30, 0.60, 0.80]Output: 0.612The probability of having (Coin1,Coin2,Coin3) = (Head,Head,Head) is 0.3×0.6×0.8 = 0.144;The probability of having (Coin1,Coin2,Coin3) = (Tail,Head,Head) is 0.7×0.6×0.8 = 0.336;The probability of having (Coin1,Coin2,Coin3) = (Head,Tail,Head)is 0.3×0.4×0.8 = 0.096;The probability of having (Coin1,Coin2,Coin3) = (Head,Head,Tail)is 0.3×0.6×0.2 = 0.036Thus, the probability of having more heads than tails is 0.144 + 0.336 + 0.096 + 0.036 = 0.612

Solution Approach

解決方法

Dynamic Programming

動(dòng)態(tài)編程

Since the problem can be solved with recursion with time complexity of O(2^n) which is not accepted as there are other better approach to solve the problem with time complexity of O(n*n);

由于可以用O(2 ^ n)的時(shí)間復(fù)雜度遞歸來解決問題,這是不被接受的,因?yàn)檫€有其他更好的方法可以解決O(n * n)的時(shí)間復(fù)雜度問題;

Let's assume prob[i][j] to be the probability of getting j heads with first i coins. To get j heads at the ith position, there are two possibilities:

假設(shè)prob [i] [j]是第一個(gè)i個(gè)硬幣獲得j個(gè)頭的概率。 為了使j個(gè)頭位于第i 個(gè)位置,有兩種可能性:

If the number of heads till (i - 1) coins is equal to j then a tail comes at ith. If the number of heads till (i - 1) coins is equal to (j - 1) then a head comes at ith position.

如果直到(i-1)個(gè)硬幣的正面數(shù)等于j, i 個(gè)背面為尾數(shù) 。 如果直到(i-1)個(gè)硬幣的正面數(shù)等于(j-1),則正面在 i 個(gè)位置。

The probability of occurring jth head at ith coin toss depends on the number of heads in (i-1)th coins, if (i-1) coin have (j-1) head then jth coin occurs at ith coin(p[i]), and if (i-1) coins have already j coins then at jth toss tails come(1-p[i]).

第i個(gè)硬幣拋擲第j個(gè)硬幣的概率取決于第(i-1) 個(gè)硬幣的硬幣的數(shù)目,如果(i-1)個(gè)硬幣具有(j-1) 個(gè)硬幣的硬幣,則第j個(gè)硬幣出現(xiàn)在 i 個(gè)硬幣( p [ i] ),如果(i-1)個(gè)硬幣已經(jīng)有j個(gè)硬幣,則第j個(gè)拋尾出現(xiàn)( 1-p [i] )。

Pseudo Code:

偽代碼:

// p[] is the probability array, // n is the size of array. solve(p[],n):// declare prob[n+1][n+1] array of having required head.prob[n+1][n+1] // no head out of 1 coinsprob[1][0]=1-p[0] // one head out of one coinsprob[1][1]=p[0] for(i=2;i<=n;i++)// probability of having no heads.prob[i][0]=prob[i-1][0]*(1-p[i]) for(i=2;i<=n;i++)for(j=1;j<=i;j++)prob[i][j]=prob[i-1][j-1]*p[i-1]+prob[i-1][j]*(1-p[i-1])// probability of having jth head can take // place in (i-1) coins or at (i)th coin.sum=0for(i=n/2+n%2;i<=n;i++)//probability of heads>tails.sum+=prob[n][i] return sum

Time complexity: In worst case O(n*n)

時(shí)間復(fù)雜度 :在最壞的情況下O(n * n)

C++ Implementation:

C ++實(shí)現(xiàn):

#include <bits/stdc++.h> using namespace std;typedef double ll;int main() {cout << "Enter number of test cases: ";int t;cin >> t;while (t--) {cout << "Enter the number of coins: ";int n;cin >> n;ll p[n];cout << "Enter the probabilities of head: ";for (int i = 0; i < n; i++)cin >> p[i];ll prob[n + 1][n + 1];// probability of one heads with one coinsprob[1][1] = p[0];// probability of no heads with one coinsprob[1][0] = 1 - p[0];for (int i = 2; i <= n; i++)// probability of no heads with i coins.prob[i][0] = prob[i - 1][0] * (1 - p[i - 1]);for (int i = 2; i <= n; i++) {for (int j = 1; j <= i; j++)// probability of head at ith coin it it occurs at(i-1)// then just multiply with tail probability otherwise// multiply with head probability.prob[i][j] = prob[i - 1][j - 1] * p[i - 1] + prob[i - 1][j] * (1 - p[i - 1]);}ll sum = 0;for (int i = n / 2 + n % 2; i <= n; i++)// take those probability which have more heads than tails.sum += prob[n][i];cout << "The probability of heads more than the tails is: ";cout << setprecision(10) << sum << "\n";}return 0; }

Output

輸出量

Enter number of test cases: 3 Enter the number of coins: 5 Enter the probabilities of head: 0.42 0.01 0.42 0.99 0.42 The probability of heads more than the tails is: 0.3821815872 Enter the number of coins: 3 Enter the probabilities of head: 0.30 0.60 0.80 The probability of heads more than the tails is: 0.612 Enter the number of coins: 1 Enter the probabilities of head: 0.50 The probability of heads more than the tails is: 0.5

翻譯自: https://www.includehelp.com/icp/probability-of-getting-more-number-of-heads-than-tails-if-coins-are-tossed.aspx

拋硬幣正面期望

總結(jié)

以上是生活随笔為你收集整理的抛硬币正面期望_如果抛硬币,正面的数量多于反面的可能性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。