【发布时间】:2020-12-05 22:03:47
【问题描述】:
所以我有一个问题,从 3 天前开始我就一直坚持下去。
您想参加 6/49 彩票只有一个中奖变体(简单),并且您想知道您的中奖几率:
-I 类(6 个数字)
-在 II 类(5 个数字)
-在类别 III(4 个数字)
编写一个控制台应用程序,从输入的总球数、提取的球数和类别中获取,然后如果您使用一个简单的变体,则以小数点后 10 位的精度打印获胜几率。
输入:
40
5
二
结果我必须打印:
0.0002659542
static void Main(string[] args)
{
int numberOfBalls = Convert.ToInt32(Console.ReadLine());
int balls = Convert.ToInt32(Console.ReadLine());
string line = Console.ReadLine();
int theCategory = FindCategory(line);
double theResult = CalculateChance(numberOfBalls, balls, theCategory);
Console.WriteLine(theResult);
}
static int FindCategory (string input)
{
int category = 0;
switch (input)
{
case "I":
category = 1;
break;
case "II":
category = 2;
break;
case "III":
category = 3;
break;
default:
Console.WriteLine("Wrong category.");
break;
}
return category;
}
static int CalculateFactorial(int x)
{
int factorial = 1;
for (int i = 1; i <= x; i++)
factorial *= i;
return factorial;
}
static int CalculateCombinations(int x, int y)
{
int combinations = CalculateFactorial(x) / (CalculateFactorial(y) * CalculateFactorial(x - y));
return combinations;
}
static double CalculateChance(int a, int b, int c)
{
double result = c / CalculateCombinations(a, b);
return result;
}
现在我的问题是:我很确定我必须使用组合。对于使用组合,我需要使用阶乘。但是在组合公式中,我使用了相当大的阶乘,所以我的数字被截断了。我的第二个问题是我并不真正了解我与这些类别有什么关系,而且我很确定我在这种方法上也做错了。我是编程新手,所以请和我一起裸露。我可以用基本的东西来解决这个问题,比如条件、方法、原语、数组。
【问题讨论】:
-
你应该找到有用的阅读,你不应该划分
ints:stackoverflow.com/questions/661028/…
标签: c# algorithm combinations factorial