From ef9dfb5d8bfa705a0c056a2652d298e5ebb763fb Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Wed, 22 Feb 2023 20:18:04 +0200 Subject: Solved a bunch of problems --- codeforces/recursionTest/recursionTest.cpp | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'codeforces/recursionTest/recursionTest.cpp') 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 -#include +#include using namespace std; string ans; -bool solve(vector v,int idx, int target, int sum, string opts) { - if(idx == v.size() - 1) { - if(sum == target){ +bool solve(vector 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 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; } -- cgit v1.2.3