0
나는 고전적인 knapsap problem을 풀려고 노력했다. 그러나 나는 잘못된 대답을 108
으로 받고 있습니다. 내가 잘못한 것을 알아낼 수있게 도와 주시겠습니까? 여기에서 나는 recursion
을 사용하고있다.배낭 동적 프로그래밍 디버깅
무게 제한은 10
대답 5 + 3 + 2 ==> 25 + 15 + 14 = 54
public class KnapSack {
public static int[] weight={6,5,4,3,2};
public static int[] value={12,25,24,15,14};
public static void main(String[] args) {
System.out.println(c(0,0,10));
}
public static int c(int currentElement,int currentValue,int currentReamainder){
int p = 0;
if(currentReamainder<=0) return currentValue;
for(int i=currentElement;i<weight.length;i++){
if(currentReamainder<weight[i]) return currentValue;
p = Math.max(value[i]+c(i+1,currentValue+value[i],currentReamainder-weight[i]),c(i+1,currentValue,currentReamainder))
}
return p;
}
}
업데이트 : 나는의 가중치를 인쇄하려면 어떻게해야합니까 최적 해?
나쁘지. 알았다. 고맙습니다. – user3693167
아직도 내 대답이 잘못되었습니다. 정답의 두 배인 108을줍니다. – user3693167
새 질문으로 편집 됨. 고맙습니다. – user3693167