1. // 面试题14:剪绳子
  2. // 题目:给你一根长度为n绳子,请把绳子剪成m段(m、n都是整数,n>1并且m≥1)。
  3. // 每段的绳子的长度记为k[0]、k[1]、……、k[m]。k[0]*k[1]*…*k[m]可能的最大乘
  4. // 积是多少?例如当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此
  5. // 时得到最大的乘积18。
  6.  
  7. #include <iostream>
  8. #include <cmath>
  9.  
  10. // ====================动态规划====================
  11. int maxProductAfterCutting_solution1(int length)
  12. {
  13. if(length < )
  14. return ;
  15. if(length == )
  16. return ;
  17. if(length == )
  18. return ;
  19.  
  20. int* products = new int[length + ];
  21. products[] = ;
  22. products[] = ;
  23. products[] = ;
  24. products[] = ;
  25.  
  26. int max = ;
  27. for(int i = ; i <= length; ++i)
  28. {
  29. max = ;
  30. for(int j = ; j <= i / ; ++j)
  31. {
  32. int product = products[j] * products[i - j];
  33. if(max < product)
  34. max = product;
  35.  
  36. products[i] = max;
  37. }
  38. }
  39.  
  40. max = products[length];
  41. delete[] products;
  42.  
  43. return max;
  44. }
  45.  
  46. // ====================贪婪算法====================
  47. int maxProductAfterCutting_solution2(int length)
  48. {
  49. if(length < )
  50. return ;
  51. if(length == )
  52. return ;
  53. if(length == )
  54. return ;
  55.  
  56. // 尽可能多地减去长度为3的绳子段
  57. int timesOf3 = length / ;
  58.  
  59. // 当绳子最后剩下的长度为4的时候,不能再剪去长度为3的绳子段。
  60. // 此时更好的方法是把绳子剪成长度为2的两段,因为2*2 > 3*1。
  61. if(length - timesOf3 * == )
  62. timesOf3 -= ;
  63.  
  64. int timesOf2 = (length - timesOf3 * ) / ;
  65.  
  66. return (int) (pow(, timesOf3)) * (int) (pow(, timesOf2));
  67. }
  68.  
  69. // ====================测试代码====================
  70. void test(const char* testName, int length, int expected)
  71. {
  72. int result1 = maxProductAfterCutting_solution1(length);
  73. if(result1 == expected)
  74. std::cout << "Solution1 for " << testName << " passed." << std::endl;
  75. else
  76. std::cout << "Solution1 for " << testName << " FAILED." << std::endl;
  77.  
  78. int result2 = maxProductAfterCutting_solution2(length);
  79. if(result2 == expected)
  80. std::cout << "Solution2 for " << testName << " passed." << std::endl;
  81. else
  82. std::cout << "Solution2 for " << testName << " FAILED." << std::endl;
  83. }
  84.  
  85. void test1()
  86. {
  87. int length = ;
  88. int expected = ;
  89. test("test1", length, expected);
  90. }
  91.  
  92. void test2()
  93. {
  94. int length = ;
  95. int expected = ;
  96. test("test2", length, expected);
  97. }
  98.  
  99. void test3()
  100. {
  101. int length = ;
  102. int expected = ;
  103. test("test3", length, expected);
  104. }
  105.  
  106. void test4()
  107. {
  108. int length = ;
  109. int expected = ;
  110. test("test4", length, expected);
  111. }
  112.  
  113. void test5()
  114. {
  115. int length = ;
  116. int expected = ;
  117. test("test5", length, expected);
  118. }
  119.  
  120. void test6()
  121. {
  122. int length = ;
  123. int expected = ;
  124. test("test6", length, expected);
  125. }
  126.  
  127. void test7()
  128. {
  129. int length = ;
  130. int expected = ;
  131. test("test7", length, expected);
  132. }
  133.  
  134. void test8()
  135. {
  136. int length = ;
  137. int expected = ;
  138. test("test8", length, expected);
  139. }
  140.  
  141. void test9()
  142. {
  143. int length = ;
  144. int expected = ;
  145. test("test9", length, expected);
  146. }
  147.  
  148. void test10()
  149. {
  150. int length = ;
  151. int expected = ;
  152. test("test10", length, expected);
  153. }
  154.  
  155. void test11()
  156. {
  157. int length = ;
  158. int expected = ;
  159. test("test11", length, expected);
  160. }
  161.  
  162. int main(int agrc, char* argv[])
  163. {
  164. test1();
  165. test2();
  166. test3();
  167. test4();
  168. test5();
  169. test6();
  170. test7();
  171. test8();
  172. test9();
  173. test10();
  174. test11();
  175.  
  176. return ;
  177. }

剑指offer——面试题14:剪绳子的更多相关文章

  1. 剑指Offer:面试题14——调整数组顺序使奇数位于偶数前面(java实现)

    问题描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 思路: 1.最简单的想法,不考虑时间复杂度,扫描数组,遇到偶数,先取出这 ...

  2. 剑指offer面试题14(Java版):调整数组顺序使奇数位于偶数的前面

    题目:输入一个整数数组.实现一个函数来调整该数组中数字的顺序.使得全部奇数位于数组的前半部分.全部偶数位于数组的后半部分. 1.基本实现: 假设不考虑时间复杂度,最简单的思路应该是从头扫描这个数组,每 ...

  3. 【剑指offer 面试题14】调整数组顺序使奇数位于偶数前面

    思路: 头尾指针,向中间遍历,依据条件交换元素. #include <iostream> using namespace std; void reOrder(int *pData, uns ...

  4. C++版 - 剑指offer面试题14: 调整数组顺序使奇数位于偶数前面

    题目: 调整数组顺序使奇数位于偶数前面 热度指数:11843 时间限制:1秒 空间限制:32768K 本题知识点: 数组 题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇 ...

  5. 【剑指offer】面试题 14. 剪绳子

    面试题 14. 剪绳子 LeetCode 题目描述 给你一根长度为 n 的绳子,请把绳子剪成 m 段(m.n 都是整数,n>1 并且 m>1),每段绳子的长度记为 k[0],k[1],·· ...

  6. 剑指offer面试题-Java版-持续更新

    最近在用Java刷剑指offer(第二版)的面试题.书中原题的代码采用C++编写,有些题的初衷是为了考察C++的指针.模板等特性,这些题使用Java编写有些不合适.但多数题还是考察通用的算法.数据结构 ...

  7. C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解

    剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...

  8. C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解

    剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题) 原书题目:0, 1, - , n-1 这n个数字排成一个圈圈,从数字0开始每次从圆圏里删除第m个数字.求出这个圈圈里剩下的最后一个数字 ...

  9. 剑指Offer:面试题15——链表中倒数第k个结点(java实现)

    问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...

随机推荐

  1. Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法

    Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法 摘自:https://blog.csdn.net/qq_31635851/article/details/ ...

  2. 黑盒测试实践-任务进度-Day03

    任务进度11-28 使用工具 selenium 小组成员 华同学.郭同学.穆同学.沈同学.覃同学.刘同学 任务进度 经过了前两天的学习任务的安排,以下是大家的任务进度: 华同学(任务1) 1.今天就接 ...

  3. .Net 数据库(SqlServer2008)的备份、还原

    //备份代码private void Backup() { SqlConnection sqlConn = new SqlConnection(strConn); strFileName = &quo ...

  4. PS插件开发plugin

    Photoshop插件开发 VC++制作Photoshop自动化插件:http://blog.sina.com.cn/s/blog_73c52fda0101c7hw.html Photoshop 的扩 ...

  5. JAVA WEB第0课

            在这学期内要掌握JAVA WAB高级网站开发的所有知识,并可以实际运用到.每周将花费20小时左右的时间来学习此门课程,每一天,在当天其他课程任务完成后将开始学习该课程,具体时间要看当天 ...

  6. 【Java】对Map按key和value分别排序

    一.什么是Map? 在讲解Map排序之前,我们先来稍微了解下map. map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. ...

  7. [转]FreeMarker使用

    copy自http://demojava.iteye.com/blog/800204 以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主 ...

  8. 最近的一些零碎知识点,jquery遍历

    1.使按钮无法点击 $(“#btn”).attr("disable",true); 2.返回上一个页面 history.back(-1); 3.$(this).siblings() ...

  9. ComicEnhancerPro 系列教程十九:用JpegQuality看JPG文件的压缩参数

    作者:马健邮箱:stronghorse_mj@hotmail.com 主页:http://www.comicer.com/stronghorse/ 发布:2017.07.23 教程十九:用JpegQu ...

  10. C# Winform 加载窗体/对象时的等待页面设计

    在设计应用程序过程中,有时候加载对象需时较长,我们可以显示一个Loading等待页面,对用户来说就比较友好了. 这个还是涉及到多线程,下面是步骤. 一.创建好Loading窗体: 一个Panel用于显 ...