代码:importjava.util.*;publicclassMain{staticintn;staticintc;staticint[]v=newint[n];staticint[]w=newint[n];staticintbestp=0;publicstaticvoidmain(String[]args){Scannerinp...
代码:
import java.util.*;
public class Main {
static int n;
static int c;
static int[] v = new int[n];
static int[] w = new int[n];
static int bestp=0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
n = input.nextInt();
c = input.nextInt();
for(int i=0; i
v[i] = input.nextInt();
for(int i=0; i
w[i] = input.nextInt();
Backtrack(0);
System.out.println(bestp);
}
public static int Bound(int i)
{
int b=0;
int j;
for(j=i;j
return b;
}
public static void Backtrack(int i){
int cw=0;
int cp=0;
if(i>=n){
bestp=cp;
return;
}
if(cw+w[i]<=c){
cw+=w[i];
cp+=v[i];
Backtrack(i+1);
cw-=w[i];
cp-=v[i];
}
if(cp+Bound(i+1) > bestp)
Backtrack(i+1);
}
}
但出现
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at javaapplication43.Main.Backtrack(Main.java:36)
at javaapplication43.Main.main(Main.java:19)
Java Result: 1
展开