Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

0

Knapsack Problem
Knapsack Problem

SIR UZAIR ISHTIAQ


AIZAZ HUSSAIN BSCS B7 080

Aizaz Hussain BSCS B7 080

Knapsack Problem

Knapsack Problem:
In this problem we are making a code in which the compiler demands user input for the
total weight and the total weight a bag can carry, the compiler will calculate the total bags
by using the formula total_weight/i-j, while I is initialized by 1 and j is initialized by 0. The
formula will execute until the result becomes zero.
Code:
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int tweight, bagweight, count;
cout<<"Enter the total weight in the store: ";
cin>>tweight;
cout<<"Enter the weight that the bag can carry: ";
cin>>bagweight;

count = -1;
int i=1, j=0;
do
{
tweight=tweight/i-j;
j++;
i++;
count++;
}
while(tweight>0);
cout<<'\n'<<"So total number of bags a theif can carry is: "<<count;
Aizaz Hussain BSCS B7 080

Knapsack Problem

getch();

Aizaz Hussain BSCS B7 080

You might also like