Files
coding-practice/legacy/808 soupServings/C#/Program.cs

56 lines
1.1 KiB
C#
Executable File

using System;
public class Solution
{
private Dictionary<(int, int), double> dp = new Dictionary<(int, int), double>();
public double SoupServings(int n)
{
if (n > 5000)
return 1.0;
n = (n + 24) / 25;
// use dynamic programming
return Helper(n, n);
}
private double Helper(int a, int b)
{
if (a <= 0 && b <= 0)
{
return 0.5;
}
if (a <= 0)
{
return 1.0;
}
if (b <= 0)
{
return 0.0;
}
if (dp.ContainsKey((a, b)))
{
return dp[(a, b)];
}
double probability = 0.25 * (
Helper(a - 4, b) +
Helper(a - 3, b - 1) +
Helper(a - 2, b - 2) +
Helper(a - 1, b - 3)
);
dp[(a, b)] = probability;
return probability;
}
}
class Program
{
static void Main()
{
var Solution = new Solution();
var ans = Solution.SoupServings(800);
Console.WriteLine(ans);
}
}