找规律如下; 可以看到从5开始就有规律了。某个数减去3 如果结果大于或等于2,就减,否则就别减了。可以根据找到的规律很容易地写出代码。
2 1 1
3 1 2
4 2 2
5 2 3
6 3 3
7 3 4
8 3 3 2
class Solution {
public int cuttingRope(int n) {
if(n<1){
return 0;
}
//找规律
int count=1;
if(n==4){return n;}
if(n==3){return 2;}
if(n==2){return 1;};
while(n-3>=2){
count*=3;
n=n-3;}
count=n*count;
return count;
}
}