3
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?

  • Did you debug the issue step by step? I bet you´ll find the problem way faster than anyone here, as you know best what your app **should** do and what it does **instead**. – MakePeaceGreatAgain Feb 02 '21 at 11:56
  • Sure, I debugged, but I still can't find out what is the problem. – Bodor Arianna Feb 02 '21 at 11:58
  • So what **exactly** happens while debugging? What does `bc` return for case `"III"`? Put a breakpoint into `bc` and see if it gets hit. If it does step line by line through your code to see where it behaves different from what you expect. We can´t say you what you expect, only you can. – MakePeaceGreatAgain Feb 02 '21 at 12:04
  • Think about: if your inputs for both `k` and `n` are odd, will `if (k == 0 || k == n)` ever become true? – 500 - Internal Server Error Feb 02 '21 at 12:34

1 Answers1

2

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.

Palle Due
  • 5,929
  • 4
  • 17
  • 32
  • The compiler knows and is trying to tell you. If your are using an older version of C# it might not know about named tupes `(int n, int k)`. – Palle Due Feb 02 '21 at 12:53