Skip to content

Commit

Permalink
collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Youpen-y committed Oct 22, 2020
1 parent 75cacab commit 821405f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
7 changes: 5 additions & 2 deletions code/bagProblem.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include<cmath>

using namespace std;

Expand Down Expand Up @@ -44,7 +45,7 @@ int main()
cin>>volume;
cout << "Please input the num of things"<<endl;
cin>>n;
thing a[n];
thing *a = new thing[n];
for(int i=0;i<n;i++)
{
int tempa,tempb;
Expand All @@ -58,7 +59,7 @@ int main()
int tempvolume=0;
int tempvalue=0;
int maxvalue=0;
for(int i=0;i<(n<<1);i++)
for(int i=0;i<pow(2,n);i++)
{

for(int j=0;j<n;j++)
Expand All @@ -72,10 +73,12 @@ int main()
}
if(tempvolume<=volume)
{
cout<<"hello1"<<endl;
if(tempvalue>maxvalue)
maxvalue=tempvalue;
}
}
delete a;
cout<<"The MAXvalue is : "<<maxvalue<<endl;
return 0;
}
Binary file modified code/bagProblem.exe
Binary file not shown.
44 changes: 44 additions & 0 deletions code/bruteforce.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* A Naive recursive implementation of
0-1 Knapsack problem */
#include <bits/stdc++.h>
using namespace std;

// A utility function that returns
// maximum of two integers
int max(int a, int b) { return (a > b) ? a : b; }

// Returns the maximum value that
// can be put in a knapsack of capacity
int knapSack(int totalWeight, int weight[], int value[], int n)
{

// Base Case
if (n == 0 || totalWeight == 0)
return 0;

// If weight of the nth item is more
// than Knapsack capacity W, then
// this item cannot be included
// in the optimal solution
if (weight[n] > totalWeight)
return knapSack(totalWeight, weight, value, n - 1);

// Return the maximum of two cases:
// (1) nth item included
// (2) not included
else
return max(
value[n] + knapSack(totalWeight - weight[n], weight, value, n - 1),
knapSack(totalWeight, weight, value, n - 1));
}

// Driver code
int main()
{
int val[] = { 42, 12, 40, 25 };
int wt[] = { 7, 3, 4, 5 };
int W = 10;
int n = sizeof(val) / sizeof(val[0]);
cout << knapSack(W, wt, val, n);
return 0;
}

0 comments on commit 821405f

Please sign in to comment.