C# 动态规划 最优解 括号匹配问题 斐波那契数列

本文探讨如何使用C#编程语言,结合动态规划算法来解决经典的括号匹配问题,同时深入解析动态规划在解决斐波那契数列上的应用,提供最优解决方案。
摘要由CSDN通过智能技术生成
  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值