C# lottery 6 from 49 algorithm

倾然丶 夕夏残阳落幕 提交于 2021-01-29 05:31:24

问题


I need to display odds to win with ten decimals if I play with just one variant, for six five and four numbers. For example I need to have this 0.0000000715 but I have this 0.0027829314 if I introduce 49,6,I. What is the problem?How can I make it work? I am a beginner and I don't know how i can obtain this 0.0000000715.

 class Program
{
    static void Main(string[] args)
    {
        int n = Convert.ToInt32(Console.ReadLine());
        int k = Convert.ToInt32(Console.ReadLine());
        string category = Console.ReadLine();
        switch (category)
        {
            case "I":
                calculate(n,k);
                break;
            case "II":
                calculate(n, k);
                break;
            case "III":
                calculate(n, k);
                break;
        }
    }
    static void calculate(int n, int k)
    {
        int nk = n - k;
       decimal count = prod(1, nk) / prod(k + 1, n);
        decimal r = prod(1, k) / prod(n - k + 1, n);
        decimal sum = count * r;
        Console.WriteLine(Math.Round(r,10));
    }

    static decimal prod(int x, int y)
    {
        decimal prod = 0;
        for(int i = x; i <= y; i++)
        {
            prod = x * y;
        }
        return prod;
    }
}

回答1:


The general solution would be bc(6,n)*bc(49-6,6-n)/bc(49, 6), where n is, 4, 5 or 6 and bc is the binomial coefficient.

Btw.: double should be enough for 10 decimal places, there is no need to use decimal.

using System;       
public class Program
{
    //bonomial coefficient
    static double bc(double n, double k)  
    {
        if (k == 0 || k == n) 
            return 1;
        return bc(n - 1, k - 1) + bc(n - 1, k); 
    } 
    public static void Main()
    {
        for(int n = 4; n <=6; ++n){
            Console.WriteLine(bc(6,n)*bc(49-6,6-n)/bc(49, 6));
        }
    }
}



回答2:


I am not sur what function you were using.

The chances of winning all 6 numbers is 1 in 13,983,816

The actual calculation is this:

49C6 = 49!/(43! x 6!) = 13983816

So the probability to win is 1 / 13,983,816 = 0.0000000715




回答3:


Your prod function should look like:

 static decimal prod(int x, int y)
    {
        decimal prod = 1;
        for(int i = x; i <= y; i++)
        {
            prod = prod * i;
        }
        return prod;
    }



回答4:


As jjj mentioned, you overwrite "prod" everytime, but you need to add it



来源:https://stackoverflow.com/questions/63104134/c-sharp-lottery-6-from-49-algorithm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!