钢条切割问题

在这里插入图片描述

自顶向下 (递归实现)

公式:
在这里插入图片描述

    class Program
    {
        static void Main(string[] args)
        {
            int n = 5;//我们要切割售卖的钢条的长度
            int[] p = { 0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };//索引代表 钢条的长度,值代表价格
            Console.WriteLine(UpDown(0, p));
            Console.WriteLine(UpDown(1, p));
            Console.WriteLine(UpDown(2, p));
            Console.WriteLine(UpDown(3, p));
            Console.WriteLine(UpDown(4, p));
            Console.WriteLine(UpDown(5, p));
            Console.WriteLine(UpDown(6, p));
            Console.WriteLine(UpDown(7, p));
            Console.WriteLine(UpDown(8, p));
            Console.WriteLine(UpDown(9, p));
            Console.WriteLine(UpDown(10, p));
            Console.ReadKey();
        }
		//缺点:影响性能,效率比较低
        public static int UpDown(int n,int[] p)//求得长度为n的最大收益
        {
            if (n == 0) return 0;
            int tempMaxPrice = 0;
            for (int i = 1; i < n + 1; i++)
            {
            	//递归
                int maxPrice = p[i] + UpDown(n - i, p);
                if (maxPrice > tempMaxPrice)
                {
                    tempMaxPrice = maxPrice;
                }
            }
            return tempMaxPrice;
        }
    }

带缓存的自顶向下

 class Program
    {
        static void Main(string[] args)
        {
        	//表示用于存储长度n的最大值
            int[] resultPrice = new int[11];//储存计算过的钢条切割后价格
 
            int[] p = { 0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };//价格表,索引值代表钢条长度,值代表价格
            Console.WriteLine(UpDown(0, p,resultPrice));
            Console.WriteLine(UpDown(1, p, resultPrice));
            Console.WriteLine(UpDown(2, p, resultPrice));
            Console.WriteLine(UpDown(3, p, resultPrice));
            Console.WriteLine(UpDown(4, p, resultPrice));
            Console.WriteLine(UpDown(5, p, resultPrice));
            Console.WriteLine(UpDown(6, p, resultPrice));
            Console.WriteLine(UpDown(7, p, resultPrice));
            Console.WriteLine(UpDown(8, p, resultPrice));
            Console.WriteLine(UpDown(9, p, resultPrice));
            Console.WriteLine(UpDown(10, p, resultPrice));
 
            Console.ReadKey();
        }
 
        /// 
        /// 带备忘录的自顶向下的切割
        /// 
        /// 
        /// 
        /// 
        /// 
        private static int UpDown(int n, int[] p,int[] result)
        {
            if (n == 0) return 0;//递归结束条件,当钢条长度为0,结束
 			
            if(result[n]!=0)//如果已经计算过最大值 则返回
            {
                return result[n];
            }
            int tempMaxPrice = 0;
            for (int i = 1; i <= n; i++)
            {
                int maxPrice = p[i] + UpDown(n - i, p,result);
                if (maxPrice > tempMaxPrice)
                {
                    tempMaxPrice = maxPrice;
                }
            }
            //表示长度为n的最大利润值
            result[n] = tempMaxPrice;//将计算的子问题价格保存
            return tempMaxPrice;
        }
    }

自底向上-动态规划

 class Program
    {
        static void Main(string[] args)
        {
            int[] resultPrice = new int[11];//储存计算过的钢条切割后价格
 
            int[] p = { 0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };//价格表,索引值代表钢条长度,值代表价格
            Console.WriteLine(BottomUp(0,p,resultPrice));
            Console.WriteLine(BottomUp(1, p, resultPrice));
            Console.WriteLine(BottomUp(2, p, resultPrice));
            Console.WriteLine(BottomUp(3, p, resultPrice));
            Console.WriteLine(BottomUp(4, p, resultPrice));
            Console.WriteLine(BottomUp(5, p, resultPrice));
            Console.WriteLine(BottomUp(6, p, resultPrice));
            Console.WriteLine(BottomUp(7, p, resultPrice));
            Console.WriteLine(BottomUp(8, p, resultPrice));
            Console.WriteLine(BottomUp(9, p, resultPrice));
            Console.WriteLine(BottomUp(10, p, resultPrice));
 
            Console.ReadKey();
        }
 		//若想取n的最大值,自底向上依次取得1,2,3,,,n-1的最大值
        private static int BottomUp(int n,int[] p,int[] result)
        {
            int tempMaxPrice = -1;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=i;j++)
                {
                    int maxPrice = p[j] + result[i - j];
 
                    if(maxPrice>tempMaxPrice)
                    {
                        tempMaxPrice = maxPrice;
                    }
                }
                result[i] = tempMaxPrice;
            }
            return result[n];
        }
    }

你可能感兴趣的:(算法学习)