feat : add description and c# solution

This commit is contained in:
2025-08-08 10:13:45 +08:00
parent c7ee7fa6a5
commit 1879561061
4 changed files with 104 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
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);
}
}