簡體   English   中英

彩票中獎機會練習

[英]Lottery winning chance exercise

所以我有一個問題,我從 3 天前開始就一直堅持下去。

您想參加 6/49 彩票只有一個中獎變體(簡單),並且您想知道您的中獎幾率:

- 在 I 類(6 個數字)

- 在 II 類(5 個數字)

- 在 III 類(4 個數字)

編寫一個控制台應用程序,從輸入的總球數、提取的球數和類別中獲取,然后如果您使用一個簡單的變體,則以小數點后 10 位的精度打印獲勝幾率。

輸入:

40

5

結果我必須打印:

0.0002659542

static void Main(string[] args)
        {
            int numberOfBalls = Convert.ToInt32(Console.ReadLine());
            int balls = Convert.ToInt32(Console.ReadLine());
            string line = Console.ReadLine();
            int theCategory = FindCategory(line);
            double theResult = CalculateChance(numberOfBalls, balls, theCategory);
            Console.WriteLine(theResult);
        }
        static int FindCategory (string input)
        {
            int category = 0;
            switch (input)
            {
                case "I":
                    category = 1;
                    break;
                case "II":
                    category = 2;
                    break;
                case "III":
                    category = 3;
                    break;
                default:
                    Console.WriteLine("Wrong category.");
                    break;
            }
            return category;
        }
        static int CalculateFactorial(int x)
        {
            int factorial = 1;
            for (int i = 1; i <= x; i++)
                factorial *= i;
            return factorial;
        }
        static int CalculateCombinations(int x, int y)
        {
            int combinations = CalculateFactorial(x) / (CalculateFactorial(y) * CalculateFactorial(x - y));
            return combinations;
        }
        static double CalculateChance(int a, int b, int c)
        {
            double result = c / CalculateCombinations(a, b);
            return result;
        }

現在我的問題是:我很確定我必須使用組合。 對於使用組合,我需要使用階乘。 但是在組合公式中,我使用了相當大的階乘,所以我的數字被截斷了。 我的第二個問題是我並不真正了解我與這些類別有什么關系,而且我很確定我在這種方法上也做錯了。 我是編程新手,所以請和我一起裸露。 我可以用基本的東西來解決這個問題,比如條件、方法、原語、arrays。

讓我們從組合學開始; 首先,達成協議:

  • a - 所有可能的數字(您的測試用例中為40
  • t - 所有取數(在您的測試用例中為5
  • c - 測試用例中的類別( 2

所以我們有

t - c + 1表示中獎號碼, c - 1表示中獎號碼。 讓我們計算組合:

所有組合:從可能a組合中取t

A = a! / t! / (a - t)! 

中獎號碼組合:從t - c + 1中獎號碼中取t個可能的號碼:

W = t! / (t - c + 1)! / (t - t + c - 1) = t! / (t - c + 1)! / (c - 1)!

丟失號碼的組合:取c - 1 a - t可能的號碼中的 1 個丟失號碼:

L = (a - t)! / (c - 1)! / (a - t - c + 1)!

類別c的所有組合,即恰好t - c + 1獲勝號碼和c - 1失敗號碼:

C = L * W

可能性:

P = C / A = L * W / A =

t! * t! (a - t)! * (a - t)! / (t - c + 1)! / (c - 1)! / (c - 1)! / (a - t- c + 1)! / a!

呃:不要讓我們為它實現一些代碼:

代碼:

// double : note, that int is too small for 40! and the like values
private static double Factorial(int value) {
  double result = 1.0;

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

  return result;
}

private static double Chances(int a, int t, int c) =>
  Factorial(a - t) * Factorial(t) * Factorial(a - t) * Factorial(t) /
    Factorial(t - c + 1) /
    Factorial(c - 1) / 
    Factorial(c - 1) /
    Factorial(a - t - c + 1) /
    Factorial(a); 

測試:

 Console.Write(Chances(40, 5, 2));

結果:

 0.00026595421332263435

編輯:

組合而言,如果C(x, y)表示“從x中取出y項”,我們有

A = C(a, t); W = C(t, t - c + 1); L = C(a - t, c - 1)

P = W * L / A = C(t, t - c + 1) * C(a - t, c - 1) / C(a, t)

Combinations代碼非常簡單; 唯一的技巧是我們返回double

// Let'g get rid of noisy "Compute": what else can we do but compute?
// Just "Combinations" without pesky prefix.
static double Combinations(int x, int y) =>
  Factorial(x) / Factorial(y) / Factorial(x - y);

private static double Chances(int a, int t, int c) =>
  Combinations(t, t - c + 1) *
  Combinations(a - t, c - 1) /
  Combinations(a, t); 

你可以擺弄解決方案

暫無
暫無

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

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