diff options
Diffstat (limited to 'codeforces/recursionTest')
| -rwxr-xr-x | codeforces/recursionTest/recursionTest.cpp | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/codeforces/recursionTest/recursionTest.cpp b/codeforces/recursionTest/recursionTest.cpp index 91e9db8..c61d519 100755 --- a/codeforces/recursionTest/recursionTest.cpp +++ b/codeforces/recursionTest/recursionTest.cpp @@ -1,27 +1,25 @@ #include <algorithm> -#include<bits/stdc++.h> +#include <bits/stdc++.h> using namespace std; string ans; -bool solve(vector<int> v,int idx, int target, int sum, string opts) { - if(idx == v.size() - 1) { - if(sum == target){ +bool solve(vector<int> v, int idx, int target, int sum, string opts) { + if (idx == v.size() - 1) { + if (sum == target) { ans = opts; return true; } return false; - } - else { + } else { int nxt = idx + 1; - bool plus = solve(v, nxt, target, sum + v[nxt],opts + "+"); + bool plus = solve(v, nxt, target, sum + v[nxt], opts + "+"); bool minus = solve(v, nxt, target, sum - v[nxt], opts + "-"); bool mult = solve(v, nxt, target, sum * v[nxt], opts + "*"); bool div = solve(v, nxt, target, sum / v[nxt], opts + "/"); return plus || minus || mult || div; } - } int main() { @@ -29,23 +27,22 @@ int main() { cin >> n >> target; vector<int> v(n); string opts = ""; - for(int &x : v) { + for (int &x : v) { cin >> x; } sort(v.begin(), v.end()); do { - if(solve(v, 0, target, v[0], opts)) { - for(int i = 0; i < v.size(); i++) { + if (solve(v, 0, target, v[0], opts)) { + for (int i = 0; i < v.size(); i++) { if (i == v.size() - 1) { cout << v[i]; - } - else { + } else { cout << v[i] << " " << ans[i] << " "; } } cout << '\n'; } - } while(next_permutation(v.begin(), v.end())); + } while (next_permutation(v.begin(), v.end())); return 0; } |
