class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 4, 1, 7, 8, 3 };//n=7
//递归最优解
int res1 = rec_opt(arr,6);//arr.Length-1全部计算最优解为15
int res2 = rec_opt(arr,4);//索引0到索引4计算最优解为12
//DP最优解
int res3 = dp_opt(arr,6);//15
int res4 = dp_opt(arr,5);//13
Console.WriteLine(res1);
Console.WriteLine(res2);
Console.WriteLine(res3);
Console.WriteLine(res4);
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
int n = 5;
sw.Start();
long number = GenBrackets(n, n);
sw.Stop();
System.Diagnostics.Stopwatch sw1 = new System.Diagnostics.Stopwatch();
sw1.Start();
long number1 = dp_GenBrackets(n);
sw1.Stop();
Console.WriteLine($"{n}对括号的插法共有{number}种");
Console.WriteLine($"{n}对括号的递归解法耗时:{sw.ElapsedMilliseconds}毫秒");
Console.WriteLine($"{n}对括号的插法共有{number1}种");
Console.WriteLine($"{n}对括号的DP解法耗时:{sw1.ElapsedMilliseconds}毫秒");
int temp = Foo(8);
int temp2 = DP_Foo(8);
Console.WriteLine(temp);
Cons
C# 动态规划 最优解 括号匹配问题 斐波那契数列
最新推荐文章于 2021-12-10 19:47:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)