您想通过一个(简单的)选项参加 49 场机会游戏中的 6 场,并且您想知道您获胜的机会

You want to participate in the 6 out of 49 game of chance with a single (simple) option and you want to know your chances of winning

代码现在运行良好。您想通过一个(简单的)选项参加 49 场机会游戏中的 6 场,并且您想知道您获胜的机会:

I 类(6 个号码) II类(5个号码) 第三类(4 个号码) 编写一个控制台应用程序来计算您赢得彩票的机会。推广适用于总球数、抽取球数和类别的任意组合的解决方案。

应用程序以总球数、开奖球数和类别为输入,然后打印出如果只玩一种变体,精确到小数点后10位的中奖赔率。

示例:

输入数据:

40
5
II

控制台会显示:

0.0002659542

公式如下:

kCj * (n-j)C(k-j) / nCk 

应该翻译成这个

k!/(j!*(k-j)!)  *  (n-j)!/((k-j)!*(n-k)!)  /  n!/(k!*(n-k)!)

这是我的代码:

using System;

class Program
{
    static void Main()
    {
        decimal totalNumbers = Convert.ToDecimal(Console.ReadLine()); // total numbers
        decimal extractedNumber = Convert.ToDecimal(Console.ReadLine()); // extracted numbers
        string numbersCategory = Console.ReadLine(); // numbers that have to be extracted
        decimal temporaryExtractedNumber; // this will be used to set the category
        const decimal first = 1;
        const decimal second = 2;
        decimal a;
        decimal b;
        decimal c;
        decimal result;
        if (numbersCategory == "I")
        {
            temporaryExtractedNumber = extractedNumber;
        }

        if (numbersCategory == "II")
        {
            temporaryExtractedNumber = extractedNumber - first;
        }

        if (numbersCategory == "III")
        {
            temporaryExtractedNumber = extractedNumber - second;
        }

        a = NCK(totalNumbers, extractedNumber, temporaryExtractedNumber);
        b = NCK(totalNumbers, totalNumbers - extractedNumber, extractedNumber - temporaryExtractedNumber);
        c = NCK(totalNumbers, totalNumbers, extractedNumber);

        result = a * b / c; // this is the magic formula
        Console.WriteLine(result.ToString("F10"));
    }

    static decimal NCK(decimal totalNumbers, decimal extractedNumber, decimal temporaryExtractedNumber)
    {
        if (extractedNumber == temporaryExtractedNumber)
        {
            return 1;
        }

        if (extractedNumber - temporaryExtractedNumber == 0)
        {
            return 1;
        }

        if (temporaryExtractedNumber == 0)
        {
            return 1;
        }

        decimal temp1;
        if (extractedNumber < temporaryExtractedNumber)
        {
            temp1 = extractedNumber;
            extractedNumber = temporaryExtractedNumber;
            temporaryExtractedNumber = temp1;
        }

        decimal kj = extractedNumber - temporaryExtractedNumber;
        if (temporaryExtractedNumber < kj)
        {
            temp1 = temporaryExtractedNumber;
            temporaryExtractedNumber = kj;
            kj = temp1;
        }

        decimal result1 = 1;
        for (decimal i = temporaryExtractedNumber + 1; i <= extractedNumber; i++)
        {
            result1 *= i;
        }

        decimal result2 = 1;
        for (decimal i = 1; i <= kj; i++)
        {
            result2 *= i;
        }

        return result1 / result2;
    }
}
       

您的公式不正确。

n[=37 中选出的 k 个号码中抽出 j 个正确号码的概率=] 可能性是:

在你的公式中,第二个二项式系数中的nk已被替换为nj,导致多算。为清楚起见,分数的分子是子集的数量,其中 j 个数字是从 k 个中奖号码和 kj个号码是从nk个未中奖号码中抽取的。

通过这个小改动,您将得到预期的概率计算。


我只觉得使用codecogs. Client-side rendering with MathJax would be a lot better, IMHO, but that's not going to happen. In case Codecogs vanishes or overloads, here's the PNG:

有点不好