繁体   English   中英

彩票中奖机会练习

[英]Lottery winning chance exercise

所以我有一个问题,我从 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;
        }

现在我的问题是:我很确定我必须使用组合。 对于使用组合,我需要使用阶乘。 但是在组合公式中,我使用了相当大的阶乘,所以我的数字被截断了。 我的第二个问题是我并不真正了解我与这些类别有什么关系,而且我很确定我在这种方法上也做错了。 我是编程新手,所以请和我一起裸露。 我可以用基本的东西来解决这个问题,比如条件、方法、原语、arrays。

让我们从组合学开始; 首先,达成协议:

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

所以我们有

t - c + 1表示中奖号码, c - 1表示中奖号码。 让我们计算组合:

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

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可能的号码中的 1 个丢失号码:

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!

呃:不要让我们为它实现一些代码:

代码:

// 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)表示“从x中取出y项”,我们有

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 t, int c) =>
  Combinations(t, t - c + 1) *
  Combinations(a - t, c - 1) /
  Combinations(a, t); 

你可以摆弄解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM