简体   繁体   中英

How to calculate the winning chance of the lottery in c#?

using System;

namespace FirstApplication
{
    class Program
    {
        public static void Main()
        {
            int n = Convert.ToInt32(Console.ReadLine());
            int k = Convert.ToInt32(Console.ReadLine());
            string category = Console.ReadLine();
            double total = 0;

                switch (category)
                {
                    case "I":
                        total = bc(k, 6) * bc(n - k, k - 6) / bc(n, k);
                        Console.WriteLine("{0:F10}", total);
                        return;
                    case "II":
                        total = bc(k, 4) * bc(n - k, k - 4) / bc(n, k);
                        Console.WriteLine("{0:F10}", total);
                        return;
                    case "III":
                        total = bc(k, 2) * bc(n - k, k - 2) / bc(n, k);
                        Console.WriteLine("{0:F10}", total);
                        return;
                }
            Console.Read();

        }
        private static double bc(decimal n, decimal k)
        {
            if (k == 0 || k == n)
                return 1;
            return bc(n - 1, k - 1) + bc(n - 1, k);
        }
    }
}



I have a problem with my code.

The exercise is the following:

You participate at the lottery 6/49 with only one winning variant(simple) and you want to know what odds of winning you have:

-at category I (6 numbers)

-at category II (5 numbers)

-at category III (4 numbers)

Write a console app which gets from input the number of total balls, the number of extracted balls, and the category, then print the odds of winning with a precision of 10 decimals if you play with one simple variant.

For example if I input:

49

6

I

The result is ok, but when I input:

45

15

III

I don't get any result.

Any suggestions what is wrong with my code?

I don't think there's anything wrong with your code. It just runs for a very long time, because you call bc with the same values again and again. I added a dictionary to store and lookup the values already calculated:

private static Dictionary<(int N, int K),double> knownValues = new Dictionary<(int N, int K),double>();
private static double bc(int n, int k)
{
    var key = (n,k);
    if (!knownValues.ContainsKey(key))
    {
       if (k == 0 || k == n)
       {
           knownValues.Add(key, 1);
       }
       else 
       {
           knownvalues.Add(key, bc(n - 1, k - 1) + bc(n - 1, k));
       }
    }
    return knownValues[key];
}

And it returns in < 6 seconds with the value 0.0364626616 for your 45,15,III input. Whether that is right, I don't know, but at least it terminates. Maybe your teacher wanted you to try this and learn about recursion and calculation times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM