[英]Lottery winning chance exercise
So I have a problem that I'm stuck on it since 3 days ago.所以我有一个问题,我从 3 天前开始就一直坚持下去。
You want to participate at the lottery 6/49 with only one winning variant(simple) and you want to know what odds of winning you have:您想参加 6/49 彩票只有一个中奖变体(简单),并且您想知道您的中奖几率:
-at category I (6 numbers) - 在 I 类(6 个数字)
-at category II (5 numbers) - 在 II 类(5 个数字)
-at category III (4 numbers) - 在 III 类(4 个数字)
Write a console app which gets from input the number of total balls, the number of extracted balls, and the category, then print the odds of winning with a precision of 10 decimals if you play with one simple variant.编写一个控制台应用程序,从输入的总球数、提取的球数和类别中获取,然后如果您使用一个简单的变体,则以小数点后 10 位的精度打印获胜几率。
Inputs:输入:
40 40
5 5
II二
Result I must print:结果我必须打印:
0.0002659542 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;
}
Now my problems: I'm pretty sure I have to use Combinations.现在我的问题是:我很确定我必须使用组合。 For using combinations I need to use Factorials.对于使用组合,我需要使用阶乘。 But on the combinations formula I'm working with pretty big factorials so my numbers get truncated.但是在组合公式中,我使用了相当大的阶乘,所以我的数字被截断了。 And my second problem is that I don't really understand what I have to do with those categories, and I'm pretty sure I'm doing wrong on that method also.我的第二个问题是我并不真正了解我与这些类别有什么关系,而且我很确定我在这种方法上也做错了。 I'm new to programming so please bare with me.我是编程新手,所以请和我一起裸露。 And I can use for this problem just basic stuff, like conditions, methods, primitives, arrays.我可以用基本的东西来解决这个问题,比如条件、方法、原语、arrays。
Let's start from combinatorics;让我们从组合学开始; first, come to terms:首先,达成协议:
a
- all possible numbers ( 40
in your test case) a
- 所有可能的数字(您的测试用例中为40
)t
- all taken numbers ( 5
in your test case) t
- 所有取数(在您的测试用例中为5
)c
- category ( 2
) in your test case c
- 测试用例中的类别( 2
)So we have所以我们有
t - c + 1
for numbers which win and c - 1
for numbers which lose. t - c + 1
表示中奖号码, c - 1
表示中奖号码。 Let's count combinations:让我们计算组合:
All combinations: take t
from a
possible ones:所有组合:从可能a
组合中取t
:
A = a! / t! / (a - t)!
Winning numbers' combinations: take t - c + 1
winning number from t
possible ones:中奖号码组合:从t - c + 1
中奖号码中取t
个可能的号码:
W = t! / (t - c + 1)! / (t - t + c - 1) = t! / (t - c + 1)! / (c - 1)!
Lost numbers' combinations: take c - 1
losing numbers from a - t
possible ones:丢失号码的组合:取c - 1
a - t
可能的号码中的 1 个丢失号码:
L = (a - t)! / (c - 1)! / (a - t - c + 1)!
All combinations with category c
, ie with exactly t - c + 1
winning and c - 1
losing numbers:类别c
的所有组合,即恰好t - c + 1
获胜号码和c - 1
失败号码:
C = L * W
Probability:可能性:
P = C / A = L * W / A =
t! * t! (a - t)! * (a - t)! / (t - c + 1)! / (c - 1)! / (c - 1)! / (a - t- c + 1)! / a!
Ugh: Not let's implement some code for it:呃:不要让我们为它实现一些代码:
Code:代码:
// double : note, that int is too small for 40! and the like values
private static double Factorial(int value) {
double result = 1.0;
for (int i = 2; i <= value; ++i)
result *= i;
return result;
}
private static double Chances(int a, int t, int c) =>
Factorial(a - t) * Factorial(t) * Factorial(a - t) * Factorial(t) /
Factorial(t - c + 1) /
Factorial(c - 1) /
Factorial(c - 1) /
Factorial(a - t - c + 1) /
Factorial(a);
Test:测试:
Console.Write(Chances(40, 5, 2));
Outcome:结果:
0.00026595421332263435
Edit:编辑:
in terms of Combinations , if C(x, y)
which means "take y
items from x
" we have就组合而言,如果C(x, y)
表示“从x
中取出y
项”,我们有
A = C(a, t); W = C(t, t - c + 1); L = C(a - t, c - 1)
and和
P = W * L / A = C(t, t - c + 1) * C(a - t, c - 1) / C(a, t)
Code for Combinations
is quite easy; Combinations
代码非常简单; the only trick is that we return double
:唯一的技巧是我们返回double
:
// Let'g get rid of noisy "Compute": what else can we do but compute?
// Just "Combinations" without pesky prefix.
static double Combinations(int x, int y) =>
Factorial(x) / Factorial(y) / Factorial(x - y);
private static double Chances(int a, int t, int c) =>
Combinations(t, t - c + 1) *
Combinations(a - t, c - 1) /
Combinations(a, t);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.