问题描述:给定一个钢条的长度,求最大效益和切割方法
长度和价格的关系如下:
length 1 2 3 4 5 6 7 8 9 10
price 1 5 8 9 10 17 17 20 24 30
com.dynamic; { []price; length; []r; []s; CutRod( []price){ .priceprice; } momoizedCutRod( length){ r [length]; ( i;ilength;i){ r[i]; } memoizedCutRodAux(length,r); } memoizedCutRodAux( length, []r){ (r[length]){ r[length]; } q; (length){ q; }{ (lengthprice.length){ qprice[price.length]; }{ qprice[length]; } (lengthprice.length){ ( i;iprice.length;i){ qMath.max(q, price[i]memoizedCutRodAux(lengthi,r)); } }{ ( i;ilength;i){ qMath.max(q, price[i]memoizedCutRodAux(lengthi,r)); } } } r[length]q; q; } bottomUpCutRod( length){ r [length]; r[]; ( j;jlength;j){ q; (jprice.length){ qprice[price.length]; }{ qprice[j]; } ( i;ij;i){ qMath.max(q, price[i]r[ji]); } r[j]q; } r[length]; } extendBottomUpCutRod( length){ r [length]; s [length]; r[]; ( j;jlength;j){ q; (jprice.length){ qprice[price.length]; }{ qprice[j]; } ( i;ij;i){ (qprice[i]r[ji]){ qprice[i]r[ji]; s[j]i; } } r[j]q; } r[length]; } printSolution( length){ extendBottomUpCutRod(length); nlength; (n){ System.out.print(s[n]); nns[n]; } System.out.println(); } main([] args) { []price []{,,,,,,,,,,}; CutRod cut CutRod(price); System.out.println(cut.extendBottomUpCutRod()); cut.printSolution(); System.out.println(cut.extendBottomUpCutRod()); cut.printSolution(); System.out.println(cut.extendBottomUpCutRod()); cut.printSolution(); System.out.println(cut.extendBottomUpCutRod()); cut.printSolution(); System.out.println(); System.out.println(cut.bottomUpCutRod()); System.out.println(); System.out.println(cut.momoizedCutRod()); } }