给定N个容器,其中N个容器由A个副本组成,N个容器由B个副本组成。我们需要在N个容器中排列数字,以使随机选择一个容器并抽取一个A副本的概率最大。
例子:
Input : N = 1
Output : 0.5,
Input : N = 2
Output : 0.667
我们需要在N个容器中寻找编号为A的N个副本和编号为B的N个副本的最佳安排。
Pi = (Favorable Outcomes) / (Total Outcomes)
where Pi denotes the probability of an event i.
因此,为了最大化这一点,我们要么使分母(即总结果)最小化,要么使分子(即总有利结果总数)最大化,并使另一个保持恒定。
考虑一种可能的安排,在第一个(N-1)个容器中我们只放置数字A的副本,这样第一个(N-1)个容器中包含(N-1)个数字A的副本,而没有数字B的副本。然后将遗留的数字A副本和数字N的N副本放在最后一个容器中。
Probability (copy of A from any of the first (N-1) containers) = Pn-1 containers = 1
Probability (copy of A from Nth container) = PN = 1⁄(N+1)
Pmax = PN-1 containers * (N – 1) + PN
∴ Pmax = N / (N + 1)
C++
// CPP program to find maximum probability of
// getting a copy 'A' from N
#include
using namespace std;
// Returns the Maximum probability for Drawing
// 1 copy of number A from N containers with N
// copies each of numbers A and B
double calculateProbability(int N)
{
// Pmax = N/(N+1)
double probability = (double)N / (N + 1);
return probability;
}
int main()
{
int N;
double probabilityMax;
// 1. N = 1
N = 1;
probabilityMax = calculateProbability(N);
cout << "Maximum Probability for N = "
<< N << " is, " << setprecision(4)
<< fixed << probabilityMax << endl;
// 2. N = 2
N = 2;
probabilityMax = calculateProbability(N);
cout << "Maximum Probability for N = "
<< N << " is, " << setprecision(4)
<< fixed << probabilityMax << endl;
// 3. N = 10
N = 10;
probabilityMax = calculateProbability(N);
cout << "Maximum Probability for N = "
<< N << " is, " << setprecision(4)
<< fixed << probabilityMax << endl;
return 0;
}
Java
// Java program to find maximum probability of
// getting a copy 'A' from N
class GFG {
// Returns the Maximum probability for Drawing
// 1 copy of number A from N containers with N
// copies each of numbers A and B
static double calculateProbability(int N)
{
// Pmax = N/(N+1)
double probability = (double)N / (N + 1);
return probability;
}
// Driver code
public static void main(String[] args)
{
int N;
double probabilityMax;
// 1. N = 1
N = 1;
probabilityMax = calculateProbability(N);
System.out.println("Maximum Probability for"
+ " N = " + N + " is, " + Math.round(
probabilityMax * 10000.0) / 10000.0);
// 2. N = 2
N = 2;
probabilityMax = calculateProbability(N);
System.out.println("Maximum Probability for N = "
+ N + " is, " + Math.round(
probabilityMax * 10000.0) / 10000.0);
// 3. N = 10
N = 10;
probabilityMax = calculateProbability(N);
System.out.println("Maximum Probability for N = "
+ N + " is, " + Math.round(
probabilityMax * 10000.0) / 10000.0);
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to find maximum
# probability of getting a copy 'A' from N
# Returns the Maximum probability for
# Drawing 1 copy of number A from N
# containers with N copies each of
# numbers A and B
def calculateProbability(N):
# Pmax = N / (N+1)
probability = N / (N + 1)
return probability
# Driver code
# 1. N = 1
N = 1
probabilityMax = calculateProbability(N)
print("Maximum Probability for N = ",
N , "is, %.4f" %probabilityMax)
# 2. N = 2
N = 2
probabilityMax = calculateProbability(N);
print("Maximum Probability for N =",
N, "is, %.4f" %probabilityMax)
# 3. N = 10
N = 10
probabilityMax = calculateProbability(N);
print("Maximum Probability for N =",
N,"is, %.4f" %probabilityMax)
# This code is contributed by Anant Agarwal.
C#
// C# program to find maximum probability of
// getting a copy 'A' from N
using System;
class GFG {
// Returns the Maximum probability for Drawing
// 1 copy of number A from N containers with N
// copies each of numbers A and B
static double calculateProbability(int N)
{
// Pmax = N/(N+1)
double probability = (double)N / (N + 1);
return probability;
}
//Driver code
public static void Main ()
{
int N;
double probabilityMax;
// 1. N = 1
N = 1;
probabilityMax = calculateProbability(N);
Console.WriteLine("Maximum Probability for N = "
+ N + " is, " +
Math.Round(probabilityMax * 10000.0) / 10000.0);
// 2. N = 2
N = 2;
probabilityMax = calculateProbability(N);
Console.WriteLine("Maximum Probability for N = "
+ N + " is, " +
Math.Round(probabilityMax * 10000.0) / 10000.0);
// 3. N = 10
N = 10;
probabilityMax = calculateProbability(N);
Console.WriteLine("Maximum Probability for N = "
+ N + " is, " +
Math.Round(probabilityMax * 10000.0) / 10000.0);
}
}
// This code is contributed by Anant Agarwal.
PHP
Javascript
输出:
Maximum Probability for N = 1 is, 0.5000
Maximum Probability for N = 2 is, 0.6667
Maximum Probability for N = 10 is, 0.9091
上面程序的时间复杂度是O(1)。