动态规划 割绳子

public class Offer14 {
    static int maxProduct(int n) {
		if(n<2) {
			return 0;
		}
		if(n==2) {
			return 1;
		}
		if(n==3) {
			return 2;
		}
		
		int[]products=new int[n+1];
		products[0]=0;
		products[1]=1;
		products[2]=2;
		products[3]=3;
		int max=0;
		
		for(int i=4;i<=n;i++) {
		    max=0;
			for(int j=0;j<=i/2;j++) {
				int product=products[j]*products[i-j];
				if(product>max) {
					max=product;
					products[i]=max;
				}
			}
		}
		
		return products[n];
    	
    }
	public static void main(String[] args) {
		 System.out.println(maxProduct(5));

	}

}


猜你喜欢

转载自blog.csdn.net/qq_39147516/article/details/79501128