求指定钢条长度的最大价格
来源:互联网 发布: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; } } }
阅读全文
0 0
- 求指定钢条长度的最大价格
- 求匹配的括号的最大长度
- 求对称子字符串的最大长度
- 求对称字符串的最大长度
- 求对称字符串的最大长度
- coding - 求对称字符串的最大长度
- 求对称字符串的最大长度
- 求字符串可匹配的最大长度
- 求对称子字符串的最大长度
- 求对称字符串的最大长度算法
- 求字符串中最大的回文长度
- 求最大回文子串的长度
- 求最大公共子序列的长度
- 求一个序列的最大连续长度
- manacher求最大回文长度
- 数字组合 求最大长度
- 求符合条件的最大子串的长度
- 求两个字符串最大的公共字串的长度
- Zabbix监控nginx性能
- 4_tinyos下的亮灯实验-BLINK
- luogu1219 [八皇后] 搜索
- 【bzoj2839】【集合计数】容斥原理+线性求阶乘逆元小技巧
- 媒体查询(@media)
- 求指定钢条长度的最大价格
- system和sys
- 炒股学习笔记各种名词
- Mybatis入门到精通-2、Mybatis体系结构
- OSI七层与TCP/IP五层网络架构详解
- 数据结构学习笔记(14)---插入排序
- Gson用户指南(中文翻译)
- Java学习心得之面向对象(二)
- python 类的__slots__属性