【发布时间】:2020-11-21 09:09:34
【问题描述】:
我还有一个关于彩票的问题。我必须提出这个问题:“您想以一种变体参加 49 次机会中的 6 次游戏,并且您想知道您将赢得什么机会:I 类(6 个数字),II 类(5 个数字),类别 III(4 个数字)。编写一个应用程序,接收球总数、抽出的球数作为输入数据,然后在使用单一变体时以小数点后 10 位的精度打印获胜机会”。我的问题是:计算这个的公式是什么?我试图找到那个公式,但我没有找到。一个例子是 40、5 和 II(5 个数字),结果是 0.0002659542 或 45、15,类别 III 是 0.0000001324。我需要提一下我是初学者。我的代码正在运行,但仅适用于 49 中的 6。
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int k = Convert.ToInt32(Console.ReadLine());
string extract = Console.ReadLine();
int category1 = category(extract);
switch (category1)
{
case 6:
calculateTheOddsToWin(n, k, extract);
break;
case 5:
calculateTheOddsToWin(n, k, extract);
break;
case 4:
calculateTheOddsToWin(n, k, extract);
break;
}
}
static void calculateTheOddsToWin(int n , int k , string extract)
{
double comb = combination(n, k);
decimal solution =(decimal)( 1 / comb);
decimal round = Math.Round(solution,10);
Console.WriteLine(round);
}
static double combination(int n, int k)
{
double factN = factorialN(n);
double factK = factorialK(k);
double factNK = substractFactorialNK(n, k);
double combination = factN / (factNK * factK);
return combination;
}
static double factorialN(int n)
{
double factorialN = 1;
for(int i = 1; i <= n; i++)
{
factorialN *= i;
}
return factorialN;
}
static double factorialK( int k)
{
double factorialK = 1;
for (int i = 1; i <= k; i++)
{
factorialK *= i;
}
return factorialK;
}
static double substractFactorialNK(int n, int k)
{
double factorialNK = 1;
int substract = n - k;
for (int i = 1; i <= substract; i++)
{
factorialNK *= i;
}
return factorialNK;
}
static int category(string extract)
{
if(extract == "I")
{
return 6;
}else if(extract == "II")
{
return 5;
}else if(extract == "III")
{
return 4;
}
else
{
return -1;
}
}
【问题讨论】:
-
仅供参考,你的 factorialN 和 factorialK 是相同的,你可以用一个“factorial”方法替换它们
-
@HansKesting 我做了这个改进。 ty
-
前几天java也有同样的问题,stackoverflow.com/questions/63104134/…
-
阶乘增长如此之快和高,以至于您无法将其存储在
int中。int通常介于 -2 147 483 648 和 2 147 483 647 之间。 -
它不适用于 40,5,而 II 类我的结果是 0.0000531474,我需要这个结果 0.0002659542 @jjj。我实际上放了一个双,因为这样可以存储一个大数字
标签: c# algorithm math probability