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

来源:互联网 发布:nginx location root 编辑:程序博客网 时间:2024/04/30 11:40

带有重复计算:

 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;            }        }    }