给定一个函数foo()以相等的概率返回1到5的整数,仅使用foo()编写一个函数以相等的概率返回1到7的整数。最小化对foo()方法的调用次数。另外,不允许使用任何其他库函数,也不允许使用浮点运算。
解决方案 :
我们知道foo()返回1到5的整数。如何确保1到7的整数以相等的概率出现?
如果我们以某种方式以相等的概率生成从1到7的整数(例如7、14、21,…),则可以使用模除以7,然后加1以得到等号从1到7的数字可能性。
我们可以使用以下表达式从1到21产生相等的概率。
5*foo() + foo() -5
让我们看看如何使用上面的表达式。
1.对于第一个foo()的每个值,第二个foo()的值可以有5种可能的组合。因此,总共可能有25种组合。
2.上式返回的值范围是1到25,每个整数恰好出现一次。
3.如果方程的值小于22,则将模除以7,然后加1。否则,再次递归调用该方法。因此,返回每个整数的概率变为1/7。
下面的程序显示该表达式返回从1到25的每个整数一次。
C++
#include
int main()
{
int first, second;
for ( first=1; first<=5; ++first )
for ( second=1; second<=5; ++second )
printf ("%d \n", 5*first + second - 5);
return 0;
}
Java
// Java code to demonstrate
// expression returns each integer
// from 1 to 25 exactly once
class GFG {
public static void main(String[] args)
{
int first, second;
for ( first=1; first<=5; ++first )
for ( second=1; second<=5; ++second )
System.out.printf ("%d \n", 5*first + second - 5);
}
}
// This code is contributed by
// Smitha Dinesh Semwal
Python3
# Python3 code to demonstrate
# expression returns each integer
# from 1 to 25 exactly once
if name == '__main__':
for first in range(1, 6):
for second in range(1, 6):
print(5 * first + second - 5)
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# code to demonstrate expression returns
// each integer from 1 to 25 exactly once
using System;
class GFG {
public static void Main()
{
int first, second;
for ( first = 1; first <= 5; ++first )
for ( second = 1; second <= 5; ++second )
Console.WriteLine ( 5*first + second - 5);
}
}
// This code is contributed by Sam007.
PHP
Javascript
C++
// C++ program to Generate integer from
// 1 to 5 with equal probability
#include
// given method that returns 1 to 5 with equal probability
int foo()
{
// some code here
}
int my_rand() // returns 1 to 7 with equal probability
{
int i;
i = 5*foo() + foo() - 5;
if (i < 22)
return i%7 + 1;
return my_rand();
}
// Driver code
int main()
{
printf ("%d ", my_rand());
return 0;
}
Java
// Java program to Generate integer from
// 1 to 5 with equal probability
class GfG
{
// given method that returns 1 to 5 with equal probability
static int foo()
{
// some code here
return 0;
}
// returns 1 to 7 with equal probability
public static int my_rand()
{
int i;
i = 5*foo() + foo() - 5;
if (i < 22)
return i%7 + 1;
return my_rand();
}
// Driver code
public static void main (String[] args) {
System.out.println(my_rand());
}
}
Python3
# Python3 program to Generate integer
# from 1 to 5 with equal probability
# given method that returns 1 to 5
# with equal probability
def foo():
# some code here
return 0;
# returns 1 to 7 with equal probability
def my_rand():
i = 0;
i = (5 * foo()) + (foo() - 5);
if (i < 22):
if(i < 0):
return (i % 7 - 7) + 1;
else:
return (i % 7) + 1;
return my_rand();
# Driver code
if __name__ == '__main__':
print(my_rand());
# This code contributed by PrinciRaj1992
C#
// C# program to Generate integer from
// 1 to 5 with equal probability
using System;
class GfG
{
// given method that returns 1 to 5 with equal probability
static int foo()
{
// some code here
return 0;
}
// returns 1 to 7 with equal probability
public static int my_rand()
{
int i;
i = 5*foo() + foo() - 5;
if (i < 22)
return i%7 + 1;
return my_rand();
}
// Driver code
public static void Main () {
Console.Write(my_rand()+"\n");
}
}
PHP
Javascript
输出 :
1
2
.
.
24
25
下面的程序描述了如何使用foo()以相等的概率返回1到7。
C++
// C++ program to Generate integer from
// 1 to 5 with equal probability
#include
// given method that returns 1 to 5 with equal probability
int foo()
{
// some code here
}
int my_rand() // returns 1 to 7 with equal probability
{
int i;
i = 5*foo() + foo() - 5;
if (i < 22)
return i%7 + 1;
return my_rand();
}
// Driver code
int main()
{
printf ("%d ", my_rand());
return 0;
}
Java
// Java program to Generate integer from
// 1 to 5 with equal probability
class GfG
{
// given method that returns 1 to 5 with equal probability
static int foo()
{
// some code here
return 0;
}
// returns 1 to 7 with equal probability
public static int my_rand()
{
int i;
i = 5*foo() + foo() - 5;
if (i < 22)
return i%7 + 1;
return my_rand();
}
// Driver code
public static void main (String[] args) {
System.out.println(my_rand());
}
}
Python3
# Python3 program to Generate integer
# from 1 to 5 with equal probability
# given method that returns 1 to 5
# with equal probability
def foo():
# some code here
return 0;
# returns 1 to 7 with equal probability
def my_rand():
i = 0;
i = (5 * foo()) + (foo() - 5);
if (i < 22):
if(i < 0):
return (i % 7 - 7) + 1;
else:
return (i % 7) + 1;
return my_rand();
# Driver code
if __name__ == '__main__':
print(my_rand());
# This code contributed by PrinciRaj1992
C#
// C# program to Generate integer from
// 1 to 5 with equal probability
using System;
class GfG
{
// given method that returns 1 to 5 with equal probability
static int foo()
{
// some code here
return 0;
}
// returns 1 to 7 with equal probability
public static int my_rand()
{
int i;
i = 5*foo() + foo() - 5;
if (i < 22)
return i%7 + 1;
return my_rand();
}
// Driver code
public static void Main () {
Console.Write(my_rand()+"\n");
}
}
的PHP
Java脚本
输出:
-4