Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int x, s, n, st[20], c[20], fr[10];
- void makeCif() {
- while (x) {
- fr[x % 10] = 1;
- x /= 10;
- }
- for (int i = 0; i <= 9; i++) {
- if (fr[i])
- c[++n] = i;
- }
- }
- void tipar(int k) {
- for (int i = 1; i <= k; i++)
- cout << c[st[i]] << ' ';
- cout << endl;
- }
- int valid(int k) {
- return 1;
- }
- int sol(int k) {
- if (k > n)
- return 0;
- int sum = 0;
- for (int i = 1; i <= k; i++) {
- sum += c[st[i]];
- }
- return (s == sum);
- }
- void bktr() {
- int k = 1;
- st[k] = 0;
- while (k > 0) {
- if (st[k] < n) {
- st[k]++;
- if (valid(k)) {
- if (sol(k))
- tipar(k);
- k++;
- st[k] = st[k - 1];
- }
- }
- else
- k--;
- }
- }
- int main() {
- cin >> x >> s;
- makeCif();
- bktr();
- return 0;
- }
Add Comment
Please, Sign In to add comment