Dadas dos monedas que tienen probabilidad de obtener cara p% y q% respectivamente, la tarea es determinar la probabilidad de obtener dos caras consecutivas después de elegir monedas al azar entre las monedas dadas.
Ejemplos:
Entrada: p = 33, q = 66
Salida: 0,550000000000000
Entrada: p = 33, q = 66
Salida: 0,5500000000000000
Enfoque:
dado que ambas monedas no son idénticas , se utilizará el teorema de Bayes para obtener la probabilidad deseada.
Como las monedas se elegirán al azar, se puede elegir cualquiera de ellas, por lo que p y q se incluirán en el cálculo. Después de aplicar el teorema de Bayes, la respuesta requerida será (p * p + q * q) / (p + q) porque si se elige la primera moneda, entonces la probabilidad de obtener ambas caras espalda con espalda es p * p y lo mismo para la segunda moneda
Es una aplicación del teorema de Bayes.
P(B | A) = P(A |^| B) / P(A) = (1/2 * p * p + 1/2 * q * q) / (1/2 * p + 1/2 * q) = (p * p + q * q) / (p + q) donde;
P(B) = probabilidad de obtener cara en el segundo lanzamiento,
P(A) = probabilidad de obtener cara en el primer lanzamiento y
P(A |^| B) = probabilidad de obtener cara en ambos lanzamientos.
Entonces, P(B | A) es la probabilidad de sacar cara en el segundo tiro si sabemos que salimos cara
en el primero.
Aquí A, B denota la primera y la segunda moneda.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to get the probability // of getting two consecutive heads #include <bits/stdc++.h> using namespace std; // Function to return the probability // of getting two consecutive heads double getProbability(double p, double q) { p /= 100; q /= 100; // Formula derived from Bayes's theorem double probability = (p * p + q * q) / (p + q); return probability; } // Driver code int main() { double p, q; // given the probability of getting // a head for both the coins p = 80; q = 40; cout << fixed << setprecision(15) << getProbability(p, q) << endl; return 0; }
Java
// Java program to get the probability // of getting two consecutive heads import java.io.*; class GFG { // Function to return the probability // of getting two consecutive heads static double getProbability(double p, double q) { p /= 100; q /= 100; // Formula derived from Bayes's theorem double probability = (p * p + q * q) / (p + q); return probability; } // Driver code public static void main (String[] args) { double p, q; // given the probability of getting // a head for both the coins p = 80; q = 40; System.out.println( getProbability(p, q)); } } // This code is contributed by anuj_67..
Python 3
# Python 3 program to get the probability # of getting two consecutive heads # Function to return the probability # of getting two consecutive heads def getProbability(p, q): p /= 100 q /= 100 # Formula derived from Bayes's theorem probability = (p * p + q * q) / (p + q) return probability # Driver code if __name__ == "__main__": # given the probability of getting # a head for both the coins p = 80 q = 40 print(getProbability(p, q)) # This code is contributed # by ChitraNayal
C#
// C# program to get the probability // of getting two consecutive heads using System; class GFG { // Function to return the probability // of getting two consecutive heads static double getProbability(double p, double q) { p /= 100; q /= 100; // Formula derived from Bayes's theorem double probability = (p * p + q * q) / (p + q); return probability; } // Driver code public static void Main () { double p, q; // given the probability of getting // a head for both the coins p = 80; q = 40; Console.WriteLine( getProbability(p, q)); } } // This code is contributed by inder_verma..
PHP
<?php // PHP program to get the probability // of getting two consecutive heads // Function to return the probability // of getting two consecutive heads function getProbability($p, $q) { $p /= 100; $q /= 100; // Formula derived from // Bayes's theorem $probability = ($p * $p + $q * $q) / ($p + $q); return $probability; } // Driver code // given the probability of getting // a head for both the coins $p = 80; $q = 40; echo getProbability($p, $q); // This code is contributed // by Shivi_Aggarwal ?>
Javascript
<script> // Javascript program to get the probability // of getting two consecutive heads // Function to return the probability // of getting two consecutive heads function getProbability(p, q) { p /= 100; q /= 100; // Formula derived from Bayes's theorem let probability = (p * p + q * q) / (p + q); return probability; } // Driver code // given the probability of getting // a head for both the coins let p = 80.0; let q = 40.0; document.write(getProbability(p, q).toPrecision(15)); </script>
0.666666666666667
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)