[英]C# lottery 6 from 49 algorithm
如果我只玩一个变体,六个五和四个数字,我需要显示以十位小数获胜的赔率。 例如,我需要这个 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;
}
}
我不知道您使用的是什么 function。
赢得所有 6 个号码的机会是 1 分之 13,983,816
实际计算是这样的:
49C6 = 49!/(43!x 6!) = 13983816
所以获胜的概率是 1 / 13,983,816 = 0.0000000715
您的产品 function 应如下所示:
static decimal prod(int x, int y)
{
decimal prod = 1;
for(int i = x; i <= y; i++)
{
prod = prod * i;
}
return prod;
}
一般解决方案是bc(6,n)*bc(49-6,6-n)/bc(49, 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));
}
}
}
正如 jjj 提到的,您每次都覆盖“prod”,但您需要添加它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.