您将获得一个代表有偏硬币的函数foo()。调用foo()时,它以60%的概率返回0,以40%的概率返回1。编写一个新函数,以50%的概率返回0和1。您的函数应仅使用foo(),而不能使用其他库方法。
解决方案:
我们知道foo()以60%的概率返回0。我们如何确保以50%的概率返回0和1?
解决方案与此职位相似。如果我们可以通过某种方式以相等的概率获得两个案例,那么我们就完成了。我们两次调用foo()。两个调用都将以60%的概率返回0。因此,两次调用foo()会以相等的概率生成两对(0,1)和(1,0)。让我们看看如何。
(0,1):两次调用foo()的结果为0后接1的概率= 0.6 * 0.4 = 0.24
(1,0):从两次foo()调用中得到1后跟0的概率= 0.4 * 0.6 = 0.24
因此,这两种情况出现的可能性相等。想法是只考虑上述两种情况,一种情况下返回0,另一种情况下返回1。对于其他情况[(0,0)和(1,1)],请重复进行直到遇到以上两种情况中的任何一种。
下面的程序描述了如何使用foo()以相等的概率返回0和1。
C++
#include
using namespace std;
int foo() // given method that returns 0
// with 60% probability and 1 with 40%
{
// some code here
}
// returns both 0 and 1 with 50% probability
int my_fun()
{
int val1 = foo();
int val2 = foo();
if (val1 == 0 && val2 == 1)
return 0; // Will reach here with
// 0.24 probability
if (val1 == 1 && val2 == 0)
return 1; // Will reach here with
// 0.24 probability
return my_fun(); // will reach here with
// (1 - 0.24 - 0.24) probability
}
// Driver Code
int main()
{
cout << my_fun();
return 0;
}
// This is code is contributed
// by rathbhupendra
C
#include
int foo() // given method that returns 0 with 60%
// probability and 1 with 40%
{
// some code here
}
// returns both 0 and 1 with 50% probability
int my_fun()
{
int val1 = foo();
int val2 = foo();
if (val1 == 0 && val2 == 1)
return 0; // Will reach here with 0.24 probability
if (val1 == 1 && val2 == 0)
return 1; // // Will reach here with 0.24
// probability
return my_fun(); // will reach here with (1 - 0.24 -
// 0.24) probability
}
int main()
{
printf("%d ", my_fun());
return 0;
}
Java
import java.io.*;
class GFG {
// Given method that returns 0
// with 60% probability and 1 with 40%
static int foo()
{
// some code here
}
// Returns both 0 and 1 with 50% probability
static int my_fun()
{
int val1 = foo();
int val2 = foo();
if (val1 == 0 && val2 == 1)
return 0; // Will reach here with
// 0.24 probability
if (val1 == 1 && val2 == 0)
return 1; // Will reach here with
// 0.24 probability
return my_fun(); // will reach here with
// (1 - 0.24 - 0.24) probability
}
// Driver Code
public static void main(String[] args)
{
System.out.println(my_fun());
}
}
// This code is contributed by ShubhamCoder
Python3
# Python3 program for the
# above approach
def foo():
# Some code here
pass
# Returns both 0 and 1
# with 50% probability
def my_fun():
val1, val2 = foo(), foo()
if val1 ^ val2:
# Will reach here with
# (0.24 + 0.24) probability
return val1
# Will reach here with
# (1 - 0.24 - 0.24) probability
return my_fun()
# Driver Code
if __name__ == '__main__':
print(my_fun())
# This code is contributed by sgshah2
C#
using System;
class GFG {
// given method that returns 0
// with 60% probability and 1 with 40%
static int foo()
{
// some code here
}
// returns both 0 and 1 with 50% probability
static int my_fun()
{
int val1 = foo();
int val2 = foo();
if (val1 == 0 && val2 == 1)
return 0; // Will reach here with
// 0.24 probability
if (val1 == 1 && val2 == 0)
return 1; // Will reach here with
// 0.24 probability
return my_fun(); // will reach here with
// (1 - 0.24 - 0.24) probability
}
// Driver Code
static public void Main() { Console.Write(my_fun()); }
}
// This is code is contributed
// by ShubhamCoder
PHP
Javascript