Dados cuatro enteros a , b , c y d . Los jugadores A y B intentan marcar un penalti. La probabilidad de que A dispare al blanco es a/b mientras que la probabilidad de que B dispare al blanco es c/d . Gana el jugador que primero marca el penalti. La tarea es encontrar la probabilidad de que A gane el partido.
Ejemplos:
Entrada: a = 1, b = 3, c = 1, d = 3
Salida: 0,6
Entrada: a = 1, b = 2, c = 10, d = 11
Salida: 0,52381
Planteamiento: Si consideramos las variables K = a/b como la probabilidad de que A dispare al blanco y R = (1 – (a/b)) * (1 – (c/d)) como la probabilidad de que tanto A como B ambos fallando el objetivo.
Por tanto, la solución forma una progresión Geométrica K * R 0 + K * R 1 + K * R 2 + ….. cuya suma es (K / 1 – R) . Después de poner los valores de K y R obtenemos la fórmula como K * (1 / (1 – (1 – r) * (1 – k))) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the probability of A winning double getProbability(int a, int b, int c, int d) { // p and q store the values // of fractions a / b and c / d double p = (double)a / (double)b; double q = (double)c / (double)d; // To store the winning probability of A double ans = p * (1 / (1 - (1 - q) * (1 - p))); return ans; } // Driver code int main() { int a = 1, b = 2, c = 10, d = 11; cout << getProbability(a, b, c, d); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the probability // of A winning static double getProbability(int a, int b, int c, int d) { // p and q store the values // of fractions a / b and c / d double p = (double) a / (double) b; double q = (double) c / (double) d; // To store the winning probability of A double ans = p * (1 / (1 - (1 - q) * (1 - p))); return ans; } // Driver code public static void main(String[] args) { int a = 1, b = 2, c = 10, d = 11; System.out.printf("%.5f", getProbability(a, b, c, d)); } } // This code contributed by Rajput-Ji
Python3
# Python3 implementation of the approach # Function to return the probability # of A winning def getProbability(a, b, c, d) : # p and q store the values # of fractions a / b and c / d p = a / b; q = c / d; # To store the winning probability of A ans = p * (1 / (1 - (1 - q) * (1 - p))); return round(ans,5); # Driver code if __name__ == "__main__" : a = 1; b = 2; c = 10; d = 11; print(getProbability(a, b, c, d)); # This code is contributed by Ryuga
C#
// C# implementation of the approach using System; class GFG { // Function to return the probability // of A winning public static double getProbability(int a, int b, int c, int d) { // p and q store the values // of fractions a / b and c / d double p = (double) a / (double) b; double q = (double) c / (double) d; // To store the winning probability of A double ans = p * (1 / (1 - (1 - q) * (1 - p))); return ans; } // Driver code public static void Main(string[] args) { int a = 1, b = 2, c = 10, d = 11; Console.Write("{0:F5}", getProbability(a, b, c, d)); } } // This code is contributed by Shrikant13
PHP
<?php // PHP implementation of the approach // Function to return the probability // of A winning function getProbability($a, $b, $c, $d) { // p and q store the values // of fractions a / b and c / d $p = $a / $b; $q = $c / $d; // To store the winning probability of A $ans = $p * (1 / (1 - (1 - $q) * (1 - $p))); return round($ans,6); } // Driver code $a = 1; $b = 2; $c = 10; $d = 11; echo getProbability($a, $b, $c, $d); // This code is contributed by chandan_jnu ?>
Javascript
<script> // JavaScript implementation of the approach // Function to return the probability // of A winning function getProbability(a , b , c , d) { // p and q store the values // of fractions a / b and c / d var p = a / b; var q = c / d; // To store the winning probability of A var ans = p * (1 / (1 - (1 - q) * (1 - p))); return ans; } // Driver code var a = 1, b = 2, c = 10, d = 11; document.write( getProbability(a, b, c, d).toFixed(5)); // This code contributed by aashish1995 </script>
0.52381
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por akhand_mishra y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA