题解 | 剪绳子

剪绳子

https://www.nowcoder.com/practice/57d85990ba5b440ab888fc72b0751bf8

import java.util.*;


public class Solution {
    // 尽可能多的分为2和3,有3分3,没3分2,同时要排除1
    public int cutRope (int n) {
        // write code here
        int num_three = 0;
        if(n % 3 == 0){
            num_three = n / 3;
            return (int) Math.pow(3,num_three);
		// 当余1时,这个1可以与一个3组成两个2,所以*4
        }else if(n % 3 == 1){
            num_three = n / 3 - 1;
            return (int) Math.pow(3,num_three) * 4;
        }else if(n % 3 == 2){
            num_three = n / 3;
            return (int) Math.pow(3,num_three) * 2;
        }
        return 0;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务