Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int main()
- {
- int dataset[100][100];
- int item,i,j,capacity,weight[100],profit[100];
- printf("Enter number of items: ");
- scanf("%d", &item);
- printf("Enter profit & weight respectively: \n");
- for(int k=1;k<=item;k++)
- {
- scanf("%d %d", &profit[k],&weight[k]);
- }
- printf("Enter Capacity: ");
- scanf("%d", &capacity);
- for(i=0;i<=capacity;++i)
- {
- dataset[0][i]=0;
- }
- for(i=0;i<=item;++i)
- {
- dataset[i][0]=0;
- }
- for(i=1;i<=item;++i)
- {
- for(j=1;j<=capacity;++j)
- {
- if(weight[i]<=j)
- {
- if((dataset[i-1][j-weight[i]]+profit[i])>dataset[i-1][j])
- {
- dataset[i][j]=(dataset[i-1][j-weight[i]]+profit[i]);
- }
- else
- {
- dataset[i][j]=dataset[i-1][j];
- }
- }
- else
- {
- dataset[i][j]=dataset[i-1][j];
- }
- }
- }
- printf("Profit: %d", dataset[item][capacity]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement