12 | #剪绳子#
剪绳子
http://www.nowcoder.com/practice/57d85990ba5b440ab888fc72b0751bf8
/* 找规律求解,不管剪多少次,都会归结到2和3上,重点是3上,因为2*2*2小于3*3 */
class Solution {
public:
int cutRope(int number) {
int res;
int a = number / 3;
int b = number % 3;
if (b == 0) {
res = pow(3, a);
} else if (b == 1) {
res = 2 * 2 * pow(3, a - 1);
} else {
res = 2 * pow(3, a);
}
return res;
}
};
每日算法 文章被收录于专栏
每日算法、玩转技术、聪明理财、幸福生活!