Derga

Untitled

May 30th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cstdint>
  3. #include <iostream>
  4. #include <string>
  5. #include <queue>
  6. #include <vector>
  7.  
  8. using namespace std;
  9. using ll = long long;
  10.  
  11. const vector<string> tests = { /*"x+1=2", "x+2=1", "x-1=2", "x-2=1",
  12. "1+x=2", "2+x=1", "1-x=2", "2-x=1",
  13. "1+2=x", "2+1=x", "1-2=x",*/ "2-1=x" };
  14.  
  15. int Solution1(const string& equation) {
  16. int x_pos = 0;
  17. for (int i = 0; i < equation.size(); ++i) {
  18. if (equation[i] == 'x') x_pos = i;
  19. }
  20.  
  21.  
  22. if (x_pos == 0) {
  23. int a = equation[2] - '0';
  24. int b = equation[4] - '0';
  25. if (equation[1] == '-') {
  26. return b + a;
  27. } else {
  28. return b - a;
  29. }
  30. }
  31. else if (x_pos == 2) {
  32. int a = equation[0] - '0';
  33. int b = equation[4] - '0';
  34. if (equation[1] == '-') {
  35. return a - b;
  36. } else {
  37. return b - a;
  38. }
  39. }
  40. else if (x_pos == 4) {
  41. int a = equation[0] - '0';
  42. int b = equation[2] - '0';
  43. if (equation[1] == '-') {
  44. return a - b;
  45. } else {
  46. return a + b;
  47. }
  48. }
  49. }
  50.  
  51. int Solution2(const string& s) {
  52. vector <string> v = { "" };
  53. vector <string> nu;
  54. vector <string> for_x;
  55. for (ll i = 0; i < s.size(); i++)
  56. {
  57. if (s[i] == '=')
  58. v.push_back(to_string(s[i]));
  59. else
  60. {
  61. if (s[i] == 'x' && i == 0)
  62. for_x.push_back("1");
  63. else if (s[i + 1] == 'x' && s[i] == '+')
  64. for_x.push_back("1");
  65. else if (s[i + 1] == 'x' && s[i] == '-')
  66. for_x.push_back("-1");
  67. else if (s[i + 1] == 'x' && (s[i] != 'x' && s[i] != '+' && s[i] != '-' && s[i] != '=') && i == 0)
  68. for_x.push_back(to_string(s[i]));
  69. else if (s[i + 1] == 'x' && (s[i] != 'x' && s[i] != '+' && s[i] != '-' && s[i] != '=') && s[i - 1] == '+')
  70. for_x.push_back(to_string(s[i]));
  71. else if (s[i + 1] == 'x' && (s[i] != 'x' && s[i] != '+' && s[i] != '-' && s[i] != '=') && s[i - 1] == '-')
  72. {
  73. for_x.push_back("-");
  74. for_x.push_back(to_string(s[i]));
  75. }
  76. else if (s[i] != '+' && s[i] != '-' && s[i] != '=' && s[i] != 'x' && i == 0)
  77. {
  78. v.push_back("-");
  79. v.push_back(to_string(s[i]));
  80. }
  81. 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] != '-'))
  82. v.push_back(v[v.size() - 1] + s[i - 1] + s[i] + s[i + 1]);
  83. else if (s[i + 1] != '+' && s[i + 1] != '-' && s[i + 1] != '=' && s[i + 1] == 'x' && s[i] == '-' && i == 0)
  84. {
  85. v.push_back("+");
  86. v.push_back(to_string(s[i + 1]));
  87. }
  88. else if (s[i] != 'x' && s[i] != '+' && s[i] != '-' && s[i] != '=' && i == s.size() - 1)
  89. {
  90. v.push_back(to_string(s[i]));
  91. break;
  92. }
  93. else if (s[i] != 'x' && (s[i + 1] != 'x' && s[i + 1] != '+' && s[i + 1] != '-' && s[i + 1] != '=') && s[i] == '-')
  94. {
  95. v.push_back("+");
  96. v.push_back(to_string(s[i + 1]));
  97. }
  98. else if (s[i] != 'x' && (s[i + 1] != 'x' && s[i + 1] != '+' && s[i + 1] != '-' && s[i + 1] != '=') && s[i] == '+')
  99. {
  100. v.push_back("-");
  101. v.push_back(to_string(s[i + 1]));
  102. }
  103. 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)
  104. {
  105. v[v.size() - 1] += to_string(s[i]);
  106. }
  107. else if (s[i] != 'x' && s[i] != '+' && s[i] != '-' && s[i] != '=' && v.size() == 1)
  108. {
  109. v.push_back("-");
  110. v.push_back(to_string(s[i]));
  111. }
  112. }
  113. }
  114. string sum = "";
  115. string sum1 = "";
  116. for (ll j = 0; j < v.size(); j++)
  117. {
  118. if (v[j] != "61")
  119. sum += v[j];
  120. else
  121. {
  122. for (ll h = j + 1; h < v.size(); h++)
  123. sum1 += v[h];
  124. break;
  125. }
  126. }
  127. string sum_x = "";
  128. for (ll u = 0; u < for_x.size(); u++)
  129. {
  130. sum_x += for_x[u];
  131. }
  132. return (stoi(sum) + stoi(sum1)) / stoi(sum_x);
  133. }
  134.  
  135.  
  136.  
  137. int main() {
  138. for (const string& test : tests) {
  139. if (Solution1(test) == Solution2(test)) continue;
  140. cout << test << ' ' << Solution2(test) << ' ' << Solution1(test) << '\n';
  141. }
  142.  
  143. return 0;
  144. }
  145.  
  146. /*
  147. test1
  148. x+5=7
  149.  
  150. 2
  151.  
  152. test2
  153. 3-x=9
  154.  
  155. -6
  156. */
Advertisement
Add Comment
Please, Sign In to add comment