题解:swj社会摇成佛第六课

题目链接

solution:

dp

f[i]表示lhy小伙身长为i,能砍得的最优解

f[i]=f[j]*f[i-j];

public class CuttingRope01 {
    
    public static void main(String[] args) {
        CuttingRope01 rope = new CuttingRope01();
        Scanner sca = new Scanner(System.in);
        int length = sca.nextInt();
        System.out.println(rope.maxResult(length));
    }
    
    public int maxResult(int length) {
        if(length < 0)  return -1;
        int result[] = new int[length+1];
        if(length < 2) return 0;
        if(length == 2) return 1;
        if(length == 3) return 2;
        result[0] = 0;
        result[1] = 1;
        result[2] = 2;
        result[3] = 3;
        for(int i = 4; i <= length; i++) {
            int  max = i;
            for(int j = 1; j <= i/2; j++) {
                int temp = result[j]*result[i-j];
                if(temp > max) {
                    max = temp;
                }
            }
            result[i] = max;
        }
        
        return result[length];
    } 
    
}

你可能感兴趣的:(题解:swj社会摇成佛第六课)