码迷,mamicode.com
首页 > 其他好文 > 详细

rod cutting

时间:2017-06-22 00:25:59      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:pre   min   pack   int   turn   log   ide   sys   ice   

for a rod of length i the price of it si pi,to cut the rod to earn more money

package dynamic_programming;

public class rod_cutting {
    int [] r;
    public int[] BTU_rod_cutting(int[] p,int n)
    {
          r = new int[n];  //r[n] is the most money of the //length n 
        int[] s = new int[n];
        int q;
        r[0] = 0;
        for(int j = 0;j <= n-1;j++){ //all the amount
            q = -1;
            for(int i = 0;i<=j;j++){//divide
                if(q < p[i] + r[j-i]){
                q = p[i] + r[j-i];
                s[j] = i;    //record j rods how to divide
                }
                
            }
            r[j] = q;  //every time memory it
            
        }
        return s;
    }
    
    public void print(int[] p,int n){
        int[] a = BTU_rod_cutting(p,n);
        while(n>=0){
             System.out.println(a[n-1]+"");
             n = n - a[n-1];
        }
    }
}

 

rod cutting

标签:pre   min   pack   int   turn   log   ide   sys   ice   

原文地址:http://www.cnblogs.com/wujunde/p/7062070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!