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 < 2) 14 return 0; 15 if(length == 2) 16 return 1; 17 if(length == 3) 18 return 2; 19 20 int* products = new int[length + 1]; 21 products[0] = 0; 22 products[1] = 1; 23 products[2] = 2; 24 products[3] = 3; 25 26 int max = 0; 27 for(int i = 4; i <= length; ++i) 28 { 29 max = 0; 30 for(int j = 1; j <= i / 2; ++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 < 2) 50 return 0; 51 if(length == 2) 52 return 1; 53 if(length == 3) 54 return 2; 55 56 // 儘可能多地減去長度為3的繩子段 57 int timesOf3 = length / 3; 58 59 // 當繩子最後剩下的長度為4的時候,不能再剪去長度為3的繩子段。 60 // 此時更好的方法是把繩子剪成長度為2的兩段,因為2*2 > 3*1。 61 if(length - timesOf3 * 3 == 1) 62 timesOf3 -= 1; 63 64 int timesOf2 = (length - timesOf3 * 3) / 2; 65 66 return (int) (pow(3, timesOf3)) * (int) (pow(2, 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 = 1; 88 int expected = 0; 89 test("test1", length, expected); 90 } 91 92 void test2() 93 { 94 int length = 2; 95 int expected = 1; 96 test("test2", length, expected); 97 } 98 99 void test3() 100 { 101 int length = 3; 102 int expected = 2; 103 test("test3", length, expected); 104 } 105 106 void test4() 107 { 108 int length = 4; 109 int expected = 4; 110 test("test4", length, expected); 111 } 112 113 void test5() 114 { 115 int length = 5; 116 int expected = 6; 117 test("test5", length, expected); 118 } 119 120 void test6() 121 { 122 int length = 6; 123 int expected = 9; 124 test("test6", length, expected); 125 } 126 127 void test7() 128 { 129 int length = 7; 130 int expected = 12; 131 test("test7", length, expected); 132 } 133 134 void test8() 135 { 136 int length = 8; 137 int expected = 18; 138 test("test8", length, expected); 139 } 140 141 void test9() 142 { 143 int length = 9; 144 int expected = 27; 145 test("test9", length, expected); 146 } 147 148 void test10() 149 { 150 int length = 10; 151 int expected = 36; 152 test("test10", length, expected); 153 } 154 155 void test11() 156 { 157 int length = 50; 158 int expected = 86093442; 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 0; 177 }