Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cstdint>
- #include <iostream>
- #include <string>
- #include <queue>
- #include <vector>
- using namespace std;
- using ll = long long;
- const vector<string> tests = { /*"x+1=2", "x+2=1", "x-1=2", "x-2=1",
- "1+x=2", "2+x=1", "1-x=2", "2-x=1",
- "1+2=x", "2+1=x", "1-2=x",*/ "2-1=x" };
- int Solution1(const string& equation) {
- int x_pos = 0;
- for (int i = 0; i < equation.size(); ++i) {
- if (equation[i] == 'x') x_pos = i;
- }
- if (x_pos == 0) {
- int a = equation[2] - '0';
- int b = equation[4] - '0';
- if (equation[1] == '-') {
- return b + a;
- } else {
- return b - a;
- }
- }
- else if (x_pos == 2) {
- int a = equation[0] - '0';
- int b = equation[4] - '0';
- if (equation[1] == '-') {
- return a - b;
- } else {
- return b - a;
- }
- }
- else if (x_pos == 4) {
- int a = equation[0] - '0';
- int b = equation[2] - '0';
- if (equation[1] == '-') {
- return a - b;
- } else {
- return a + b;
- }
- }
- }
- int Solution2(const string& s) {
- vector <string> v = { "" };
- vector <string> nu;
- vector <string> for_x;
- for (ll i = 0; i < s.size(); i++)
- {
- if (s[i] == '=')
- v.push_back(to_string(s[i]));
- else
- {
- if (s[i] == 'x' && i == 0)
- for_x.push_back("1");
- else if (s[i + 1] == 'x' && s[i] == '+')
- for_x.push_back("1");
- else if (s[i + 1] == 'x' && s[i] == '-')
- for_x.push_back("-1");
- else if (s[i + 1] == 'x' && (s[i] != 'x' && s[i] != '+' && s[i] != '-' && s[i] != '=') && i == 0)
- for_x.push_back(to_string(s[i]));
- else if (s[i + 1] == 'x' && (s[i] != 'x' && s[i] != '+' && s[i] != '-' && s[i] != '=') && s[i - 1] == '+')
- for_x.push_back(to_string(s[i]));
- else if (s[i + 1] == 'x' && (s[i] != 'x' && s[i] != '+' && s[i] != '-' && s[i] != '=') && s[i - 1] == '-')
- {
- for_x.push_back("-");
- for_x.push_back(to_string(s[i]));
- }
- else if (s[i] != '+' && s[i] != '-' && s[i] != '=' && s[i] != 'x' && i == 0)
- {
- v.push_back("-");
- v.push_back(to_string(s[i]));
- }
- else if ((s[i] != '+' && s[i] != 'x' && s[i] != '-' && s[i] != '=') && (s[i + 1] != '+' && s[i + 1] != 'x' && s[i + 1] != '-' && s[i + 1] != '=') && (s[i - 1] != '=' && s[i - 1] != '+' && s[i - 1] != 'x' && s[i - 1] != '-'))
- v.push_back(v[v.size() - 1] + s[i - 1] + s[i] + s[i + 1]);
- else if (s[i + 1] != '+' && s[i + 1] != '-' && s[i + 1] != '=' && s[i + 1] == 'x' && s[i] == '-' && i == 0)
- {
- v.push_back("+");
- v.push_back(to_string(s[i + 1]));
- }
- else if (s[i] != 'x' && s[i] != '+' && s[i] != '-' && s[i] != '=' && i == s.size() - 1)
- {
- v.push_back(to_string(s[i]));
- break;
- }
- else if (s[i] != 'x' && (s[i + 1] != 'x' && s[i + 1] != '+' && s[i + 1] != '-' && s[i + 1] != '=') && s[i] == '-')
- {
- v.push_back("+");
- v.push_back(to_string(s[i + 1]));
- }
- else if (s[i] != 'x' && (s[i + 1] != 'x' && s[i + 1] != '+' && s[i + 1] != '-' && s[i + 1] != '=') && s[i] == '+')
- {
- v.push_back("-");
- v.push_back(to_string(s[i + 1]));
- }
- else if (s[i] != 'x' && s[i] != '+' && s[i] != '-' && s[i] != '=' && (s[i - 1] != 'x' && s[i - 1] != '+' && s[i - 1] != '-' && s[i - 1] != '=') && v.size() > 1)
- {
- v[v.size() - 1] += to_string(s[i]);
- }
- else if (s[i] != 'x' && s[i] != '+' && s[i] != '-' && s[i] != '=' && v.size() == 1)
- {
- v.push_back("-");
- v.push_back(to_string(s[i]));
- }
- }
- }
- string sum = "";
- string sum1 = "";
- for (ll j = 0; j < v.size(); j++)
- {
- if (v[j] != "61")
- sum += v[j];
- else
- {
- for (ll h = j + 1; h < v.size(); h++)
- sum1 += v[h];
- break;
- }
- }
- string sum_x = "";
- for (ll u = 0; u < for_x.size(); u++)
- {
- sum_x += for_x[u];
- }
- return (stoi(sum) + stoi(sum1)) / stoi(sum_x);
- }
- int main() {
- for (const string& test : tests) {
- if (Solution1(test) == Solution2(test)) continue;
- cout << test << ' ' << Solution2(test) << ' ' << Solution1(test) << '\n';
- }
- return 0;
- }
- /*
- test1
- x+5=7
- 2
- test2
- 3-x=9
- -6
- */
Advertisement
Add Comment
Please, Sign In to add comment