Skip to content

Instantly share code, notes, and snippets.

@sivabudh
Created December 22, 2016 08:58
Show Gist options
  • Save sivabudh/0ef8906425f9b37022f3359411dc0dd6 to your computer and use it in GitHub Desktop.
Save sivabudh/0ef8906425f9b37022f3359411dc0dd6 to your computer and use it in GitHub Desktop.
Knapsack Code
class Untitled {
public static void main(String[] args) {
System.out.println("Hello World!");
int max_w = 10;
int n = 4;
int v[] = {10, 40, 30, 50};
int w[] = { 5, 4, 6, 3};
int max_v = 0;
int total_w=0;
int total_v=0;
for(int i=0; i < n; i++){
for(int j=i+1; j < n; j++){
total_w = w[i]+w[j];
System.out.println("total_w = " + total_w);
if(total_w < max_w){
total_v = v[i]+v[j];
System.out.println("total_v = " + total_v);
if(total_v > max_v){
max_v = total_v;
System.out.println("max_v = " + max_v);
}
}
}
}
System.out.println(max_v);
System.out.print(total_w);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment