fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void find(int *pd, int n, int m, int s, char *pr, char *pr0)
  5. {
  6. if (n == 0) {
  7. if (s == m) {*pr = 0; cout<<" "<<pr0<<" = "<<s<<endl;}
  8. return;
  9. }
  10. *pr = '+'; find(pd+1, n-1, m, s+*pd, pr+1, pr0);
  11. *pr = '-'; find(pd+1, n-1, m, s-*pd, pr+1, pr0);
  12. }
  13.  
  14. int main()
  15. {
  16. int d[] = {1, 24, 2, 2, 8, 44, 3, 42, 4, 8, 9, 2, 2, 7, 9, 4, 3, 5, 2, 4, 9, 5, 3, 2, 3, 2, 1, 1}
  17. , m = 209;
  18. // int d[] = {1, 8, 4, 3, 2, 4, 8, 9, 2, 2, 7, 9, 4, 3, 5, 2, 4, 9, 5, 3, 2, 3, 2, 1, 1}
  19. // , m = 99;
  20. //int d[] = {1, 4, -2, 2}
  21. // , m = 5;
  22. const int n = sizeof(d)/sizeof(int);
  23. char r[n];
  24.  
  25. for(int i=0; i<n; i++) cout<<d[i]; cout<<endl;
  26. find(d+1, n-1, m, d[0], r, r);
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 1.56s 3340KB
stdin
Standard input is empty
stdout
1242284434248922794352495323211
 ++++++++++++++++++++++++++- = 209
 +++++++++++++++++++++++++-+ = 209