Advertisement
Josif_tepe

Untitled

Mar 18th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. int niza[105];
  7. int memo[10001];
  8. int f(int x) {
  9.     if(x == 0) {
  10.         return 0; // ni trebaat uste 0 broevi
  11.     }
  12.     if(memo[x] != -1) {
  13.         return memo[x];
  14.     }
  15.     int najmal = 2000000;
  16.     for(int i = 0; i < n; i++) {
  17.         if(x - niza[i] >= 0) {
  18.             if(najmal > f(x - niza[i]) + 1) {
  19.                 najmal = f(x - niza[i]) + 1;
  20.             }
  21.         }
  22.     }
  23.     memo[x] = najmal;
  24.     return najmal;
  25. }
  26. int main()
  27. {
  28.     cin >> n;
  29.     for(int i = 0; i < n; i++) {
  30.         cin >> niza[i];
  31.     }
  32.     int x;
  33.     cin >> x;
  34.     for(int i = 0; i <= x; i++) {
  35.         memo[i] = -1;
  36.     }
  37.     cout << f(x) << endl;
  38.     return 0;
  39. }
  40. // f(10)
  41. // f(9) + 1
  42. // f(9) --> f(8) + 1; f(7) + 1; f(4) + 1; f(2) + 1
  43. // f(8) -->
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement