Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int st[100], a[100], i, n, s, s2;
- void backtrack(int k)
- {
- if(s2 == s)
- {
- for(i = 1; i <= n; i++)
- cout << st[i] << "x ( " << a[i] << " ) ";
- cout << "\n";
- }
- else
- {
- st[k] = 0;
- while(((a[k] * st[k]) + s2 < s) && (k <= n))
- {
- st[k]++;
- s2 += (st[k] * a[k]);
- backtrack(k+1);
- s2 -= (st[k] * a[k]);
- }
- }
- }
- int main()
- {
- cin >> n >> s;
- for(int i = 1; i <= n; i++)
- cin >> a[i];
- backtrack(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement