Advertisement
J00ker

Untitled

Oct 8th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int st[100], a[100], i, n, s, s2;
  6.  
  7. void backtrack(int k)
  8. {
  9.     if(s2 == s)
  10.     {
  11.         for(i = 1; i <= n; i++)
  12.             cout << st[i] << "x ( " << a[i] << " )  ";
  13.         cout << "\n";
  14.     }
  15.     else
  16.     {
  17.         st[k] = 0;
  18.         while(((a[k] * st[k]) + s2 < s) && (k <= n))
  19.         {
  20.             st[k]++;
  21.             s2 += (st[k] * a[k]);
  22.             backtrack(k+1);
  23.             s2 -= (st[k] * a[k]);
  24.         }
  25.     }
  26. }
  27.  
  28. int main()
  29. {
  30.     cin >> n >> s;
  31.     for(int i = 1; i <= n; i++)
  32.         cin >> a[i];
  33.     backtrack(1);
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement