在网上看到很多钢条切割的代码,很多都是C++,用到结构体,内联函数,const变量,复杂......直接写了一段java简单的代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] p = {0,1,5,8,9,10,17,17,20,24,30};//对应长度是0,1,2,3,4,5,6,7,8,9,10的整段价值
int[] r = new int[n+1];//最大价值
int[] s = new int[n+1];//分段情况
Main main = new Main();
main.cutRodExtend(n,p,r,s);
sc.close();
}
private void cutRodExtend(int n,int[] p,int[] r,int[] s){
r[0] = 0;
int q;
int tmp;
for(int i = 1;i<=n ;i++){
q = -1;
for(int j = 1;j<=i;j++){
tmp = p[j] + r[i-j];
if(tmp >q){
q = tmp ;
s[i]