Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C彩票6从49算法_C#_Algorithm - Fatal编程技术网

C# C彩票6从49算法

C# C彩票6从49算法,c#,algorithm,C#,Algorithm,如果我只玩一个变体,六个五个和四个数字,我需要用十位小数来显示获胜的几率。例如,我需要这个0.0000000715,但如果我引入49,6,I,我有这个0.0027829314。有什么问题吗?我如何才能使它工作?我是一个初学者,我不知道如何才能获得这个0.0000000715 class Program { static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine());

如果我只玩一个变体,六个五个和四个数字,我需要用十位小数来显示获胜的几率。例如,我需要这个0.0000000715,但如果我引入49,6,I,我有这个0.0027829314。有什么问题吗?我如何才能使它工作?我是一个初学者,我不知道如何才能获得这个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;
    }
}

正如jjj所提到的,您每次都会覆盖prod,但是您需要添加它

我不确定您使用的是什么函数

赢得所有6个号码的几率为13983816分之一

实际计算如下:

49C6=49/43! x6!=13983816


因此获胜概率为1/13983816=0.0000000 715

您的prod函数应该如下所示:

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

通解为bc6,n*bc49-6,6-n/bc49,6,其中n为,4,5或6,bc为二项式系数

顺便说一句:双精度应该足够小数点后10位,不需要使用小数点

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

您的prod功能可能没有执行您想要的操作。循环是无用的,因为每次都会覆盖结果,并且不会重复使用。我想知道如何修复它,使结果为0.0000000715,而不是0.0027829314?我知道prod没用,但我不知道如何让它工作@jjjjprod到底应该做什么?在不知道这一点的情况下,我只能发布一个完全无关的代码来得到正确的结果,这不是基于yoursHow的,我能做什么?我是个初学者,我不知道怎么做@GuestIt是正确的,但此消息不符合回答条件,最好在原始问题中添加注释。@PascalGanaye您可以使用+=而不是=,添加注释,但这不会导致正确的结果。我尝试此解决方案,但解决方案为0.9716169748。我不知道错误在哪里如果prod是阶乘这会导致非常大的数字,你必须非常小心数据类型的可能范围和它的感知,数字对于decimal来说太大了。