求指定钢条长度的最大价格

带有重复计算:

 class Program
    {
        static void Main(string[] args)
        {
            //  索引代表 钢条的长度,值代表相应的价格
            int[] prices = { 0, 1, 5, 8, 9, 10, 11, 17, 20, 24, 30 };

            Console.WriteLine(Updown(prices, 0));
            Console.WriteLine(Updown(prices, 1));
            Console.WriteLine(Updown(prices, 2));
            Console.WriteLine(Updown(prices, 3));
            Console.WriteLine(Updown(prices, 4));
            Console.WriteLine(Updown(prices, 5));
            Console.WriteLine(Updown(prices, 6));
            Console.WriteLine(Updown(prices, 7));

            Console.ReadKey();
        }

        static int Updown(int[] prices, int meters)
        {
            if (meters == 0)
            {
                return 0;
            }
            int tempTotal = 0;

            for (int i = 1; i <= meters; i++)
            {
                int total = prices[i] + Updown(prices, meters - i);
                if (total > tempTotal)
                {
                    tempTotal = total;
                }
            }
            return tempTotal;
        }
    }


不带有重复计算:

class Program
    {
        static void Main(string[] args)
        {
            //  索引代表 钢条的长度,值代表相应的价格
            int[] prices = { 0, 1, 5, 8, 9, 10, 11, 17, 20, 24, 30 };
            //    值代表对应长度钢条的最大总价格
            int[] results = new int[prices.Length];

            Console.WriteLine(Updown(prices, results, 0));
            Console.WriteLine(Updown(prices, results, 1));
            Console.WriteLine(Updown(prices, results, 2));
            Console.WriteLine(Updown(prices, results, 3));
            Console.WriteLine(Updown(prices, results, 4));
            Console.WriteLine(Updown(prices, results, 5));
            Console.WriteLine(Updown(prices, results, 6));
            Console.WriteLine(Updown(prices, results, 7));

            Console.ReadKey();
        }

        static int Updown(int[] prices, int[] results, int meters)
        {
            if (meters == 0)
            {
                return 0;
            }
            //  备忘录
            if (results[meters] != 0)
            {
                return results[meters];
            }
            int tempTotal = 0;

            for (int i = 1; i <= meters; i++)
            {
                int total = prices[i] + Updown(prices, results, meters - i);
                if (total > tempTotal)
                {
                    tempTotal = total;
                }
            }
            results[meters] = tempTotal;
            return tempTotal;
        }
    }

自底向上且不含有重复计算:

class Program
    {
        static void Main(string[] args)
        {
            //  索引代表 钢条的长度,值代表相应的价格
            int[] prices = { 0, 1, 5, 8, 9, 10, 11, 17, 20, 24, 30 };
            //    值代表对应长度钢条的最大总价格
            int[] results = new int[prices.Length];

            BottomUp(prices, results, 5);

            for (int i = 0; i < results.Length; i++)
            {
                Console.Write(results[i] + "  ");
            }
            Console.Read();
        }

        static void BottomUp(int[] prices, int[] results, int meters)
        {
            for (int i = 1; i <= meters; i++)
            {
                int tempTotal = 0;
                for (int j = 1; j <= i; j++)
                {
                    int total = prices[j] + results[i - j];
                    if (total > tempTotal)
                    {
                        tempTotal = total;
                    }
                }
                results[i] = tempTotal;
            }
        }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值