簡體   English   中英

C# 彩票中獎幾率

[英]C# Lottery odds to win

我必須提出這個問題:“您想以一種變體參加 49 次機會中的 6 次游戲,並且您想知道您將贏得什么機會:I 類(6 個數字),II 類(5 個數字),類別 III(4 個數字)。編寫一個應用程序,接收球總數、抽出的球數作為輸入數據,然后在使用單一變體時以小數點后 10 位的精度打印獲勝機會”。 例如 40、5 和 II(5 個數字),結果為 0.0002659542。 我知道這個公式是 n!/k!*(nk)! 但我制作的程序只適用於第一類。

static void Main(string[] args)
{
    int n = Convert.ToInt32(Console.ReadLine());
    int k = Convert.ToInt32(Console.ReadLine());
    string extract = Console.ReadLine();
    int category1 = category(extract);
    switch (category1)
    {
        case 6:
            calculateTheOddsToWin(n, k, extract);
            break;
        case 5:
            calculateTheOddsToWin(n, k, extract);
            break;
        case 4:
            calculateTheOddsToWin(n, k, extract);
            break;
    }

}
static void calculateTheOddsToWin(int n, int k, string extract)
{
    double comb = combination(n, k);
    decimal solution = (decimal)(1 / comb);
    decimal round = Math.Round(solution, 10);
    Console.WriteLine(round);
}
static double combination(int n, int k)
{
    double factN = factorial(n);
    double factK = factorial(k);
    double factNK = substractFactorialNK(n, k);
    double combination = factN / (factNK * factK);
    return combination;
}

static double factorial(int n)
{
    double factorialN = 1;
    for (int i = 1; i <= n; i++)
    {
        factorialN *= i;
    }
    return factorialN;
}
static double substractFactorialNK(int n, int k)
{
    double factorialNK = 1;
    int substract = n - k;
    for (int i = 1; i <= substract; i++)
    {
        factorialNK *= i;
    }
    return factorialNK;
}
static int category(string extract)
{
    if (extract == "I")
    {
        return 6;
    }
    else if (extract == "II")
    {
        return 5;
    }
    else if (extract == "III")
    {
        return 4;
    }
    else
    {
        return -1;
    }
}

關於如何解決這個問題的任何想法?

這是一道數學題,所以經過研究,我是這樣做的: private static double Factorial(int value) { double result = 1.0;

    for (int i = 2; i <= value; ++i)
        result *= i;

    return result;
}

static double Combinations(int x, int y)
{
    return Factorial(x) / Factorial(y) / Factorial(x - y);
}
private static double Chances(int a, int t, int c)
{
    return Combinations(t, t - c + 1) *
           Combinations(a - t, c - 1) /
           Combinations(a, t);
}

public static void Main()
{
    int n = Convert.ToInt32(Console.ReadLine());
    int k=Convert.ToInt32(Console.ReadLine());
    string category=Console.ReadLine();
    if (category == "I")
    {
        int categoryInt = 1;
        Console.WriteLine(string.Format("{0:F10}", Chances(n, k, categoryInt)));
    }
    if (category == "II")
    {
        int categoryInt = 2;
        Console.WriteLine(string.Format("{0:F10}", Chances(n, k, categoryInt)));
    }
    if (category == "III")
    {
        int categoryInt = 3;
        Console.WriteLine(string.Format("{0:F10}", Chances(n, k, categoryInt)));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM