1. 程式人生 > >《劍指offer》第十四題(剪繩子)

《劍指offer》第十四題(剪繩子)

貪婪 const int mes 多少 result 分解 new ostream

// 面試題:剪繩子
// 題目:給你一根長度為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 < 2)//當大小小於3的時候可以直接得出 return 0; if (length == 2) return 1; if (length == 3) return 2; int* products = new int[length + 1]; products[
0] = 0; products[1] = 1; products[2] = 2; products[3] = 3; int max = 0; for (int i = 4; i <= length; ++i) { max = 0; for (int j = 1; j <= i / 2; ++j) { int product = products[j] * products[i - j];//products[j]和products[i - j]都是已經得到的
if (max < product) max = product; products[i] = max;//選擇存起來每個子問題的最優解,提高效率 } } max = products[length]; delete[] products; return max; } // ====================貪婪算法==================== //需要數學底子,比如下面這個就得公式證明3(n-3)>=2(n-2),n>=5。。 int maxProductAfterCutting_solution2(int length) { if (length < 2) return 0; if (length == 2) return 1; if (length == 3) return 2; // 盡可能多地減去長度為3的繩子段 int timesOf3 = length / 3; // 當繩子最後剩下的長度為4的時候,不能再剪去長度為3的繩子段。 // 此時更好的方法是把繩子剪成長度為2的兩段,因為2*2 > 3*1。 if (length - timesOf3 * 3 == 1) timesOf3 -= 1; int timesOf2 = (length - timesOf3 * 3) / 2; return (int)(pow(3, timesOf3)) * (int)(pow(2, 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 = 1; int expected = 0; test("test1", length, expected); } void test2() { int length = 2; int expected = 1; test("test2", length, expected); } void test3() { int length = 3; int expected = 2; test("test3", length, expected); } void test4() { int length = 4; int expected = 4; test("test4", length, expected); } void test5() { int length = 5; int expected = 6; test("test5", length, expected); } void test6() { int length = 6; int expected = 9; test("test6", length, expected); } void test7() { int length = 7; int expected = 12; test("test7", length, expected); } void test8() { int length = 8; int expected = 18; test("test8", length, expected); } void test9() { int length = 9; int expected = 27; test("test9", length, expected); } void test10() { int length = 10; int expected = 36; test("test10", length, expected); } void test11() { int length = 50; int expected = 86093442; test("test11", length, expected); } int main(int agrc, char* argv[]) { test1(); test2(); test3(); test4(); test5(); test6(); test7(); test8(); test9(); test10(); test11(); system("pause"); return 0; }

《劍指offer》第十四題(剪繩子)