原文:
给定三个整数 n 、 m 、 x 。任务是找到在 x 个袋子中分配 m 个物品的概率,使得第一个袋子包含 n 个物品 示例:
输入: m = 7,x =3,n = 3 输出:0.2 3 袋装 7 件的方式数为。 2 袋装 4 件物品的方式数量为。因为第一个包里有 3 样东西。 概率为// 输入: m = 9,x = 3,n = 4 /输出: 0.142857
进场: 一般来说,k 袋放置 n 件物品的方式数量为。
- 将 m 个物品保存在 x 个袋子中的方法数量为。
- 将(m-n)个物品保存在(x-1)个袋子中的方法数量为。因为第一个包包含 n 个项目。
- 概率为 / 。
以下是上述方法的实现:
c
// cpp program to find probability of
// first bag to contain n items such
// that m items are distributed among x bags
#include
using namespace std;
// function to find factorial of a number
int factorial(int n)
{
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
// function to find ncr
int ncr(int n, int r)
{
return factorial(n) / (factorial(r) * factorial(n - r));
}
// function to find probability of
// first bag to contain n items such
// that m items are distributed among x bags
float probability(int m, int n, int x)
{
return (float)(ncr(m - n - 1, x - 2) /
(ncr(m - 1, x - 1) * 1.0));
}
// driver code
int main()
{
int m = 9, x = 3, n = 4;
// function call
cout << probability(m, n, x);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to find probability of
// first bag to contain n items such
// that m items are distributed among x bags
class gfg
{
// function to find factorial of a number
public static int factorial(int n)
{
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
// function to find ncr
public static int ncr(int n, int r)
{
return factorial(n) / (factorial(r) * factorial(n - r));
}
// function to find probability of
// first bag to contain n items such
// that m items are distributed among x bags
public static float probability(int m, int n, int x)
{
return (float) (ncr(m - n - 1, x - 2) / (ncr(m - 1, x - 1) * 1.0));
}
// driver code
public static void main(string[] args)
{
int m = 9, x = 3, n = 4;
// function call
system.out.println(probability(m, n, x));
}
}
// this code is contributed by
// sanjeev2552
python 3
# python3 program to find probability of
# first bag to contain n items such
# that m items are distributed among x bags
# function to find factorial of a number
def factorial(n) :
if (n <= 1) :
return 1;
return n * factorial(n - 1);
# function to find ncr
def ncr(n, r) :
return (factorial(n) / (factorial(r) *
factorial(n - r)));
# function to find probability of
# first bag to contain n items such
# that m items are distributed among x bags
def probability(m, n, x) :
return float(ncr(m - n - 1, x - 2) /
(ncr(m - 1, x - 1) * 1.0));
# driver code
if __name__ == "__main__" :
m = 9; x = 3; n = 4;
# function call
print(probability(m, n, x));
# this code is contributed by ankitrai01
c
// c# program to find probability of
// first bag to contain n items such
// that m items are distributed among x bags
using system;
class gfg
{
// function to find factorial of a number
static int factorial(int n)
{
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
// function to find ncr
static int ncr(int n, int r)
{
return factorial(n) / (factorial(r) * factorial(n - r));
}
// function to find probability of
// first bag to contain n items such
// that m items are distributed among x bags
static float probability(int m, int n, int x)
{
return (float) (ncr(m - n - 1, x - 2) / (ncr(m - 1, x - 1) * 1.0));
}
// driver code
static void main()
{
int m = 9, x = 3, n = 4;
// function call
console.writeline(probability(m, n, x));
}
}
// this code is contributed by
// mohitkumar 29
java 描述语言
output:
0.142857
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处