输入一个数n,意义见题面。(2 <= n <= 60)
输出答案。
题目链接:
分析:
5 : 2*3
6: 3*3
7: 3*2*2
8: 3*3*2
9: 3*3*3
将绳子分成长度为3的绳子时,乘积最大。
当余数为1时,注意将最后一段长度为4的绳子分为2*2。
public class Solution { public int cutRope(int target) {
//由于n>1,m>1 所以有2,3两个特殊情况 if(target == 2){ return 1; } if(target == 3){ return 2; } if(target%3==0){ return (int)Math.pow(3,target/3); }else if(target%3==1){ return (int)Math.pow(3,target/3)/3*4; }else if(target%3==2){ return (int)Math.pow(3,target/3)*2; } return 0; } }
原文:https://www.cnblogs.com/MoonBeautiful/p/13125530.html