Solve the Equation问题及解法
2017-10-19 10:34
507 查看
问题描述:
Solve a given equation and return the value of
the form of string "x=#value". The equation contains only '+', '-' operation, the variable
its coefficient.
If there is no solution for the equation, return "No solution".
If there are infinite solutions for the equation, return "Infinite solutions".
If there is exactly one solution for the equation, we ensure that the value of
an integer.:
示例:
问题分析:
将等式分解,左边未知数,右边常量,按照一元一次方程组的求解方法求解。
过程详见代码:
class Solution {
public:
string solveEquation(string equation) {
int lx = 0,rx = 0;
int lv = 0, rv = 0;
int t = 0, sign = 1, flag = 0;
for (int i = 0; i < equation.length(); i++)
{
if (!flag)
{
if (equation[i] == '-' || equation[i] == '+' || equation[i] == '=')
{
if (i > 0 && equation[i - 1] != 'x') lv += t* sign;
if (equation[i] == '-') sign = -1;
else if (equation[i] == '+')sign = 1;
else
{
flag = 1;
sign = 1;
}
t = 0;
}
else if (equation[i] == 'x')
{
if (!i || (equation[i - 1] < '0' || equation[i - 1] > '9')) t = 1;
lx += sign * t;
t = 0;
}
else t = t * 10 + equation[i] - '0';
}
else
{
if (equation[i] == '-' || equation[i] == '+')
{
if (i > 0 && equation[i - 1] != 'x') rv += t* sign;
if (equation[i] == '-') sign = -1;
else sign = 1;
t = 0;
}
else if (equation[i] == 'x')
{
if (!i || (equation[i - 1] < '0' || equation[i - 1] > '9')) t = 1;
rx += sign * t;
t = 0;
}
else t = t * 10 + equation[i] - '0';
}
}
if (equation.back() != 'x') rv += t* sign;
lx -= rx;
rv -= lv;
if (lx == 0 && rv == 0) return "Infinite solutions";
else if (lx != 0) return "x=" + to_string(rv / lx);
else return "No solution";
}
};
Solve a given equation and return the value of
xin
the form of string "x=#value". The equation contains only '+', '-' operation, the variable
xand
its coefficient.
If there is no solution for the equation, return "No solution".
If there are infinite solutions for the equation, return "Infinite solutions".
If there is exactly one solution for the equation, we ensure that the value of
xis
an integer.:
示例:
Input: "x+5-3+x=6+x-2" Output: "x=2"
Input: "x=x" Output: "Infinite solutions"
Input: "2x=x" Output: "x=0"
Input: "2x+3x-6x=x+2" Output: "x=-1"
Input: "x=x+2" Output: "No solution"
问题分析:
将等式分解,左边未知数,右边常量,按照一元一次方程组的求解方法求解。
过程详见代码:
class Solution {
public:
string solveEquation(string equation) {
int lx = 0,rx = 0;
int lv = 0, rv = 0;
int t = 0, sign = 1, flag = 0;
for (int i = 0; i < equation.length(); i++)
{
if (!flag)
{
if (equation[i] == '-' || equation[i] == '+' || equation[i] == '=')
{
if (i > 0 && equation[i - 1] != 'x') lv += t* sign;
if (equation[i] == '-') sign = -1;
else if (equation[i] == '+')sign = 1;
else
{
flag = 1;
sign = 1;
}
t = 0;
}
else if (equation[i] == 'x')
{
if (!i || (equation[i - 1] < '0' || equation[i - 1] > '9')) t = 1;
lx += sign * t;
t = 0;
}
else t = t * 10 + equation[i] - '0';
}
else
{
if (equation[i] == '-' || equation[i] == '+')
{
if (i > 0 && equation[i - 1] != 'x') rv += t* sign;
if (equation[i] == '-') sign = -1;
else sign = 1;
t = 0;
}
else if (equation[i] == 'x')
{
if (!i || (equation[i - 1] < '0' || equation[i - 1] > '9')) t = 1;
rx += sign * t;
t = 0;
}
else t = t * 10 + equation[i] - '0';
}
}
if (equation.back() != 'x') rv += t* sign;
lx -= rx;
rv -= lv;
if (lx == 0 && rv == 0) return "Infinite solutions";
else if (lx != 0) return "x=" + to_string(rv / lx);
else return "No solution";
}
};
相关文章推荐
- poj 3239 Solution to the n Queens Puzzle n皇后问题的构造解法
- Math-640-Solve the Equation
- Predict the Winner问题及解法
- leetcode 640. Solve the Equation
- Solve the Equation
- [LeetCode] Solve the Equation 解方程
- 640. Solve the Equation
- Construct the Rectangle问题及解法
- Can you solve the equation?(二分求方程解)
- 640. Solve the Equation
- [POJ 3311]Hie with the Pie——再谈TSP问题的DP解法
- Find the Difference问题及解法
- POJ 3311 Hie with the Pie (TSP问题 状压DP解法)
- Find the Duplicate Number问题及解法
- LeetCode Weekly Contest 40(2) - Solve the Equation
- Predict the Winner一个动态规划的问题解法详解
- LeetCode 640 : Solve the Equation(python)
- Use the BFS to solve the equation problems(Case : hdu 4403 )
- [leetcode]640. Solve the Equation
- Reshape the Matrix问题及解法