40 lines
807 B
C++
40 lines
807 B
C++
|
/**
|
|||
|
* File: max_product_cutting.cpp
|
|||
|
* Created Time: 2023-07-21
|
|||
|
* Author: krahets (krahets@163.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>зֳ˻<D6B3><CBBB><EFBFBD>̰<EFBFBD><CCB0> */
|
|||
|
int maxProductCutting(int n) {
|
|||
|
// <20><> n <= 3 ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>зֳ<D0B7>һ<EFBFBD><D2BB> 1
|
|||
|
if (n <= 3) {
|
|||
|
return 1 * (n - 1);
|
|||
|
}
|
|||
|
// ̰<>ĵ<EFBFBD><C4B5>зֳ<D0B7> 3 <20><>a Ϊ 3 <20>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>b Ϊ<><CEAA><EFBFBD><EFBFBD>
|
|||
|
int a = n / 3;
|
|||
|
int b = n % 3;
|
|||
|
if (b == 1) {
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 1 ʱ<><CAB1><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> 1 * 3 ת<><D7AA>Ϊ 2 * 2
|
|||
|
return (int)pow(3, a - 1) * 2 * 2;
|
|||
|
}
|
|||
|
if (b == 2) {
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 2 ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
return (int)pow(3, a) * 2;
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0 ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
return (int)pow(3, a);
|
|||
|
}
|
|||
|
|
|||
|
/* Driver Code */
|
|||
|
int main() {
|
|||
|
int n = 58;
|
|||
|
|
|||
|
// ̰<><CCB0><EFBFBD>㷨
|
|||
|
int res = maxProductCutting(n);
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>зֳ˻<EFBFBD>Ϊ" << res << endl;
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|