Respuesta :
Answer:
see explaination
Explanation:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package knapsack;
import java.util.*;
/**
*
* atauthor Administrator
*/
public class MyKnapsack {
static int max(int a, int b) { return (a > b)? a : b; }
static int calculate(int W, int wt[], int val[], int n)
{
if (n == 0 || W == 0)
return 0;
if (wt[n-1] > W)
return calculate(W, wt, val, n-1);
else return max( val[n-1] + calculate(W-wt[n-1], wt, val, n-1),
calculate(W, wt, val, n-1)
);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.printf("|Enter value of capacity");
int Capacity =sc.nextInt();
System.out.printf("|Enter value of number of objects");
int n=sc.nextInt();
int profits[] = new int[n];
int weight[] = new int[n];
System.out.printf("|Enter values for profits");
for(int i=0;i<n;i++)
{
profits[i]=sc.nextInt();
}
System.out.printf("|Enter values for weights");
for(int j=0;j<n;j++)
{
weight[j]=sc.nextInt();
}
System.out.println(calculate(Capacity,weight, profits, n));
}
Note: change the at with the sign
}