Advertisement
allia

набор слитков

Nov 19th, 2020
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. void Print(int s, int n, int **result, int *arr, int razmer)
  7. {
  8.   int max = 0;
  9.   while (s < razmer && max < n)
  10.   {
  11.     if (result[s+1][n] == result[s][n])
  12.       s++;
  13.   else
  14.   {  
  15.     max += arr[s-1];                        
  16.     cout << s  << endl;
  17.     s++;
  18.   }
  19.   }
  20. }
  21.  
  22. int main()
  23. {
  24.  int N, W, min = INT8_MAX;
  25.  cin >> N >> W;
  26.  
  27.  int *arr = new int[N];
  28.  
  29.  for (int i = 0; i< N; i++)
  30.    cin >> arr[i];
  31.  
  32. int **result = new int*[N+1];
  33.  
  34. for (int i = 0; i<= N; i++)
  35.    result[i] = new int[W+1];
  36.  
  37. for (int j=0; j <= W; j++)
  38.    result[0][j] = 0;
  39.  
  40. for(int s=1 ;s <= N; ++s)       // s - максимальный номер предмета,
  41. {           // который можно использовать
  42.     for (int n=1; n <= W; ++n)   // n - вместимости рюкзака
  43.     {
  44.         result[s][n] = result[s-1][n];
  45.         if ( n >= arr[s] && ( result[s-1][n-arr[s]]+arr[s] > result[s][n]) )
  46.             result[s][n] = result[s-1][n-arr[s]]+arr[s];
  47.     }
  48. }
  49.  
  50. Print(1, W, result, arr, N);
  51.  
  52. for (int i=0; i<=N; i++)
  53.     {
  54.       for (int j=0; j<=W; j++)
  55.       {
  56.         cout.width(3);
  57.         cout << result[i][j] << " ";
  58.       }
  59.      cout << endl;
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement