aboutsummaryrefslogtreecommitdiff
path: root/recursionTest.cpp
diff options
context:
space:
mode:
authorOmar Magdy <omar.professional8777@gmail.com>2022-05-04 20:02:33 +0200
committerOmar Magdy <omar.professional8777@gmail.com>2022-05-04 20:02:33 +0200
commit6d7b9aae2f3f6a83cf0517ae96f3258e7191ce56 (patch)
tree55c10509964b10bdda7ec763688d63bcc928ec2f /recursionTest.cpp
parent0cef0cea2289b14264c5bf1693966c3c8cc0ca00 (diff)
downloadcompetitive-programming-6d7b9aae2f3f6a83cf0517ae96f3258e7191ce56.tar.xz
competitive-programming-6d7b9aae2f3f6a83cf0517ae96f3258e7191ce56.zip
Solved some new problems
Diffstat (limited to 'recursionTest.cpp')
-rw-r--r--recursionTest.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/recursionTest.cpp b/recursionTest.cpp
new file mode 100644
index 0000000..ae3ab32
--- /dev/null
+++ b/recursionTest.cpp
@@ -0,0 +1,47 @@
+#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){
+ ans = opts;
+ return true;
+ }
+ return false;
+ }
+ else {
+ int nxt = idx + 1;
+ 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() {
+ int n, target;
+ cin >> n >> target;
+ vector<int> v(n);
+ string opts = "";
+ for(int &x : v) {
+ cin >> x;
+ }
+ 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 {
+ cout << v[i] << " " << ans[i] << " ";
+ }
+ }
+ cout << '\n';
+ }
+
+ return 0;
+}