Advertisement
TimxAG

рюкзак

Sep 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4. int backpack(const int* weight, const int* cost, int V, int N)
  5. {
  6. int* array = new int[V+1];
  7. array[0] = 0;
  8. for(int i=1; i<=V; ++i) {
  9. array[i] = array[i-1];
  10. for(int j=0; j<N; ++j)
  11. if (weight[j]<=i)
  12. array[i] = max(array[i], array[i-weight[j]] + cost[j]);
  13. }
  14. int maxCost = array[V];
  15. delete[] array;
  16. return maxCost;
  17. }
  18. int main()
  19. {
  20. int N, V;
  21. cout << "Vvedite ob'em rukzaka: "; cin >> V;
  22. cout << "Vvedite kolichestvo predmetov: "; cin >> N;
  23. int* weight = new int[N];
  24. int* cost = new int[N];
  25. for(int i=0; i<N; ++i) {
  26. system("cls");
  27. cout << "Ostalos' predmetov: " << N-i;
  28. cout << "\n\nVvedite ves predmeta: "; cin >> weight[i];
  29. cout << "Vvedite stoimost: "; cin >> cost[i];
  30. }
  31. system("cls");
  32. std::cout << "Naibol'shaya stoimost' = " << backpack(weight, cost, V, N);
  33. delete[] weight;
  34. delete[] cost;
  35. return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement