diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2023-02-22 20:18:04 +0200 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2023-02-22 20:18:04 +0200 |
| commit | ef9dfb5d8bfa705a0c056a2652d298e5ebb763fb (patch) | |
| tree | a3ae50170dcaa0982a2680fb9df2c1a8ec179bb4 /codeforces/recursionTest | |
| parent | 6fd4c8747f798b62428ca020171596b8636a7a4c (diff) | |
| download | competitive-programming-ef9dfb5d8bfa705a0c056a2652d298e5ebb763fb.tar.xz competitive-programming-ef9dfb5d8bfa705a0c056a2652d298e5ebb763fb.zip | |
Solved a bunch of problems
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; } |
