Advertisement
askarulytarlan

рекурсия задача

Jan 8th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3.  
  4. using namespace std;
  5.  
  6. map <int, int> pr;
  7.  
  8. void ans() {
  9. while(val) {
  10. col++;
  11. b[col] = val - pr[val];
  12. val = pr[val];
  13. }
  14. if (col == 0)
  15. cout << -1;
  16. else {
  17. cout << col << endl;
  18. for (int i = 1; i <= col; i++)
  19. cout << b[col] << ' ';
  20. }
  21. }
  22.  
  23. void go(int s, int i) {
  24. if (s == 0) {
  25. ans();
  26. return;
  27. }
  28. pr[s] = pr[s - a[i]];
  29. pr[s - a[i]] = pr[s - a[i] - a[i]];
  30. go(s, i + 1);
  31. go(s - a[i], i + 1);
  32. go(s - a[i] - a[i], i + 1);
  33. }
  34.  
  35. int main() {
  36.  
  37. cin >> val >> n;
  38.  
  39. for (int i = 1; i <= n; i++)
  40. cin >> a[i];
  41.  
  42. go(val, 1);
  43.  
  44. return 0;
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement