剑指offer——面试题14:剪绳子
- // 面试题14:剪绳子
- // 题目:给你一根长度为n绳子,请把绳子剪成m段(m、n都是整数,n>1并且m≥1)。
- // 每段的绳子的长度记为k[0]、k[1]、……、k[m]。k[0]*k[1]*…*k[m]可能的最大乘
- // 积是多少?例如当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此
- // 时得到最大的乘积18。
- #include <iostream>
- #include <cmath>
- // ====================动态规划====================
- int maxProductAfterCutting_solution1(int length)
- {
- if(length < )
- return ;
- if(length == )
- return ;
- if(length == )
- return ;
- int* products = new int[length + ];
- products[] = ;
- products[] = ;
- products[] = ;
- products[] = ;
- int max = ;
- for(int i = ; i <= length; ++i)
- {
- max = ;
- for(int j = ; j <= i / ; ++j)
- {
- int product = products[j] * products[i - j];
- if(max < product)
- max = product;
- products[i] = max;
- }
- }
- max = products[length];
- delete[] products;
- return max;
- }
- // ====================贪婪算法====================
- int maxProductAfterCutting_solution2(int length)
- {
- if(length < )
- return ;
- if(length == )
- return ;
- if(length == )
- return ;
- // 尽可能多地减去长度为3的绳子段
- int timesOf3 = length / ;
- // 当绳子最后剩下的长度为4的时候,不能再剪去长度为3的绳子段。
- // 此时更好的方法是把绳子剪成长度为2的两段,因为2*2 > 3*1。
- if(length - timesOf3 * == )
- timesOf3 -= ;
- int timesOf2 = (length - timesOf3 * ) / ;
- return (int) (pow(, timesOf3)) * (int) (pow(, timesOf2));
- }
- // ====================测试代码====================
- void test(const char* testName, int length, int expected)
- {
- int result1 = maxProductAfterCutting_solution1(length);
- if(result1 == expected)
- std::cout << "Solution1 for " << testName << " passed." << std::endl;
- else
- std::cout << "Solution1 for " << testName << " FAILED." << std::endl;
- int result2 = maxProductAfterCutting_solution2(length);
- if(result2 == expected)
- std::cout << "Solution2 for " << testName << " passed." << std::endl;
- else
- std::cout << "Solution2 for " << testName << " FAILED." << std::endl;
- }
- void test1()
- {
- int length = ;
- int expected = ;
- test("test1", length, expected);
- }
- void test2()
- {
- int length = ;
- int expected = ;
- test("test2", length, expected);
- }
- void test3()
- {
- int length = ;
- int expected = ;
- test("test3", length, expected);
- }
- void test4()
- {
- int length = ;
- int expected = ;
- test("test4", length, expected);
- }
- void test5()
- {
- int length = ;
- int expected = ;
- test("test5", length, expected);
- }
- void test6()
- {
- int length = ;
- int expected = ;
- test("test6", length, expected);
- }
- void test7()
- {
- int length = ;
- int expected = ;
- test("test7", length, expected);
- }
- void test8()
- {
- int length = ;
- int expected = ;
- test("test8", length, expected);
- }
- void test9()
- {
- int length = ;
- int expected = ;
- test("test9", length, expected);
- }
- void test10()
- {
- int length = ;
- int expected = ;
- test("test10", length, expected);
- }
- void test11()
- {
- int length = ;
- int expected = ;
- test("test11", length, expected);
- }
- int main(int agrc, char* argv[])
- {
- test1();
- test2();
- test3();
- test4();
- test5();
- test6();
- test7();
- test8();
- test9();
- test10();
- test11();
- return ;
- }
剑指offer——面试题14:剪绳子的更多相关文章
- 剑指Offer:面试题14——调整数组顺序使奇数位于偶数前面(java实现)
问题描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 思路: 1.最简单的想法,不考虑时间复杂度,扫描数组,遇到偶数,先取出这 ...
- 剑指offer面试题14(Java版):调整数组顺序使奇数位于偶数的前面
题目:输入一个整数数组.实现一个函数来调整该数组中数字的顺序.使得全部奇数位于数组的前半部分.全部偶数位于数组的后半部分. 1.基本实现: 假设不考虑时间复杂度,最简单的思路应该是从头扫描这个数组,每 ...
- 【剑指offer 面试题14】调整数组顺序使奇数位于偶数前面
思路: 头尾指针,向中间遍历,依据条件交换元素. #include <iostream> using namespace std; void reOrder(int *pData, uns ...
- C++版 - 剑指offer面试题14: 调整数组顺序使奇数位于偶数前面
题目: 调整数组顺序使奇数位于偶数前面 热度指数:11843 时间限制:1秒 空间限制:32768K 本题知识点: 数组 题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇 ...
- 【剑指offer】面试题 14. 剪绳子
面试题 14. 剪绳子 LeetCode 题目描述 给你一根长度为 n 的绳子,请把绳子剪成 m 段(m.n 都是整数,n>1 并且 m>1),每段绳子的长度记为 k[0],k[1],·· ...
- 剑指offer面试题-Java版-持续更新
最近在用Java刷剑指offer(第二版)的面试题.书中原题的代码采用C++编写,有些题的初衷是为了考察C++的指针.模板等特性,这些题使用Java编写有些不合适.但多数题还是考察通用的算法.数据结构 ...
- C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解
剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
- C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解
剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题) 原书题目:0, 1, - , n-1 这n个数字排成一个圈圈,从数字0开始每次从圆圏里删除第m个数字.求出这个圈圈里剩下的最后一个数字 ...
- 剑指Offer:面试题15——链表中倒数第k个结点(java实现)
问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...
随机推荐
- Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法
Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法 摘自:https://blog.csdn.net/qq_31635851/article/details/ ...
- 黑盒测试实践-任务进度-Day03
任务进度11-28 使用工具 selenium 小组成员 华同学.郭同学.穆同学.沈同学.覃同学.刘同学 任务进度 经过了前两天的学习任务的安排,以下是大家的任务进度: 华同学(任务1) 1.今天就接 ...
- .Net 数据库(SqlServer2008)的备份、还原
//备份代码private void Backup() { SqlConnection sqlConn = new SqlConnection(strConn); strFileName = &quo ...
- PS插件开发plugin
Photoshop插件开发 VC++制作Photoshop自动化插件:http://blog.sina.com.cn/s/blog_73c52fda0101c7hw.html Photoshop 的扩 ...
- JAVA WEB第0课
在这学期内要掌握JAVA WAB高级网站开发的所有知识,并可以实际运用到.每周将花费20小时左右的时间来学习此门课程,每一天,在当天其他课程任务完成后将开始学习该课程,具体时间要看当天 ...
- 【Java】对Map按key和value分别排序
一.什么是Map? 在讲解Map排序之前,我们先来稍微了解下map. map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. ...
- [转]FreeMarker使用
copy自http://demojava.iteye.com/blog/800204 以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主 ...
- 最近的一些零碎知识点,jquery遍历
1.使按钮无法点击 $(“#btn”).attr("disable",true); 2.返回上一个页面 history.back(-1); 3.$(this).siblings() ...
- ComicEnhancerPro 系列教程十九:用JpegQuality看JPG文件的压缩参数
作者:马健邮箱:stronghorse_mj@hotmail.com 主页:http://www.comicer.com/stronghorse/ 发布:2017.07.23 教程十九:用JpegQu ...
- C# Winform 加载窗体/对象时的等待页面设计
在设计应用程序过程中,有时候加载对象需时较长,我们可以显示一个Loading等待页面,对用户来说就比较友好了. 这个还是涉及到多线程,下面是步骤. 一.创建好Loading窗体: 一个Panel用于显 ...