fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MaxN = 150;
  4. int n;
  5. int a[MaxN];
  6. void backtrack(int pos, int sum)
  7. {
  8. if(sum==n)
  9. {
  10. for (int i=1; i<=pos-1; i++)
  11. {
  12. cout << a[i] << " ";
  13. }
  14. cout << "\n";
  15. return ;
  16. }
  17. if(pos==1)
  18. {
  19. for (int i=1; i<=n; i++)
  20. {
  21. a[pos]=i;
  22. backtrack(pos+1,sum+i);
  23. }
  24. }
  25. else
  26. {
  27. for (int i=a[pos-1]+1; i<=n-sum; i+=2)
  28. {
  29. a[pos]=i;
  30. backtrack(pos+1,sum+i);
  31. }
  32. }
  33. }
  34. int main()
  35. {
  36. ios_base::sync_with_stdio(0);
  37. cin.tie(0);
  38. cin >> n;
  39. backtrack(1,0);
  40. }
  41.  
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout