标签:isp res rop nbsp sof lse 算法 ons ret
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 }
标签:isp res rop nbsp sof lse 算法 ons ret
原文地址:https://www.cnblogs.com/focus-z/p/10086929.html