彩票中奖机会练习

问题描述

所以我有一个问题,自3天前开始我就一直坚持下去。

您只想参加一个简单的中奖彩票,参加6/49彩票,您想知道您中奖的几率:

-位于类别I(6个数字)

-II类(5个数字)

-III类(4个数字)

编写一个控制台应用程序,该应用程序从输入的总球数,提取的球数和类别中获取信息,然后使用一种简单的变体游戏,以10位小数的精度打印获胜赔率。

输入:

40

5

II

结果我必须打印:

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;
        }

现在是我的问题:我很确定我必须使用组合。为了使用组合,我需要使用阶乘。但是在组合公式上,我正在使用相当大的阶乘,因此我的数字被截断了。我的第二个问题是我不太了解这些类别的用途,而且我很确定自己在该方法上也做错了。我是编程新手,所以请与我保持联系。对于这个问题,我只能使用一些基本的东西,例如条件,方法,基元,数组。

解决方法

让我们从组合学开始;首先,要达成条件:

  • a-所有可能的数字(测试用例中的40
  • t-所有带数字(在您的测试用例中为5
  • c-测试案例中的类别(2

所以我们有

t - c + 1代表获胜的数字,c - 1代表输掉的数字。让我们计算一下组合:

所有组合:从t个可能的组合中提取a

A = a! / t! / (a - t)! 

获奖个数字的组合:从t - c + 1个可能的数字中抽取t个获奖数字:

W = t! / (t - c + 1)! / (t - t + c - 1) = t! / (t - c + 1)! / (c - 1)!

丢失个数字:从c - 1个可能的数字中减去a - t个数字:

L = (a - t)! / (c - 1)! / (a - t - c + 1)!

类别为c的所有组合,即恰好有t - c + 1获胜和c - 1失落的数字:

C = L * W

概率:

P = C / A = L * W / A =

t! * t! (a - t)! * (a - t)! / (t - c + 1)! / (c - 1)! / (c - 1)! / (a - t- c + 1)! / a!

U!不让我们为其实现一些代码:

代码:

// 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); 

测试:

 Console.Write(Chances(40,5,2));

结果:

 0.00026595421332263435

编辑:

组合方面,如果C(x,y)表示“从y中提取x个项目, 有

A = C(a,t); W = C(t,t - c + 1); L = C(a - t,c - 1)

P = W * L / A = C(t,t - c + 1) * C(a - t,c - 1) / C(a,t)

Combinations的代码非常简单;唯一的窍门是我们返回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 c) =>
  Combinations(t,t - c + 1) *
  Combinations(a - t,c - 1) /
  Combinations(a,t); 

您可以fiddle解决方案