Advertisement
Md_hosen_zisad

Untitled

Nov 26th, 2018
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. main()
  5. {
  6. int i,j,n,w,b[10][10],d[10][10];
  7. int val[]={0,3,4,5,6};
  8. int wt[]={0,2,3,4,5};
  9. n=4;
  10. w=5;
  11. cout << "Given weight and benefit is shown below : " << endl;
  12. for(i=1;i<=n;i++)
  13. {
  14. cout << wt[i] << "kg = ";
  15. cout << val[i] << endl;
  16. }
  17. for(i=0;i<=n;i++)
  18. {
  19. d[i][0]=0;
  20. }
  21. for(j=0;j<=w;j++)
  22. {
  23. d[0][j]=0;
  24. }
  25. for(i=1;i<=n;i++)
  26. {
  27. for(j=1;j<=w;j++)
  28. {
  29. if(wt[i]<=j)
  30. {
  31. if(d[i-1][j]>val[i]+d[i-1][j-wt[i]])
  32. {
  33. d[i][j]=d[i-1][j];
  34. b[i][j]=0;
  35. }
  36. else
  37. {
  38. d[i][j]=val[i]+d[i-1][j-wt[i]];
  39. b[i][j]=1;
  40. }
  41. }
  42. else
  43. {
  44. d[i][j]=d[i-1][j];
  45. b[i][j]=0;
  46. }
  47. }
  48. }
  49. cout << "Maximum weight is : " << w << "kg." << endl;
  50. j=w;
  51. for(i=1;i<=n;i++)
  52. {
  53. if(b[i][j]==1)
  54. {
  55. cout << wt[i] << "kg = ";
  56. cout << val[i] << endl;
  57. }
  58. j=j-wt[i];
  59. }
  60. cout << "Maximum benefit is : " << d[n][w] << endl;
  61.  
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement