题目基本和剪绳子1相同,只不过对结果进行取模1000000007
贪心算法:
class Solution {
public int cuttingRope(int n) {
if (n < 4 && n > 1) {
return n - 1; //需要有一个长度为1的绳子(题目中要求n>1)
}
long product = 1;//乘积初始值
while (n > 4) {
product = product * 3 % 1000000007;
n -= 3;
}
return (int)(product*n%1000000007); //当n=4时1*4=4; n>4时上述连续乘积得到的新值*n(不停-3后剩下的n中的最大值4=2*2)
}
}