• [Math_Medium]640.Solve the Equation


    原题:640.Solve the Equation

    Solve a given equation and return the value of x in the form of string "x=#value". The equation contains only '+', '-' operation, the variable x and 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 x is an integer.

    example

    1. Input: "x+5-3+x=6+x-2"
      Output: "x=2"
    2. Input: "x=x"
      Output: "Infinite solutions"
    3. Input: "x=x+2"
      Output: "No solution"

    题目大意

    求解一元一次方程,且方程中只包括+,-,数字,=,x

    解题思路

    求解一元一次方程,考察字符串解析。
    对字符串从左往右进行遍历,设置sign作为+或-的标志,flag作为在方程左边还是右边的标志,首先判断str[i]是否是=,因为等号左右第一个可能会出现没有带符号的数字(x+2=3x+2,x和3x前面就没有+或者-)。再判断是否是+或者-,最后再判断是不是数字;最后,如果x的系数为0且常数也为0,则有无穷解,若仅x的系数为0,则无解;只要x的系数不为0,则有解,这里把项都移到了等号左边,所以要乘以-1.

    代码

    class Solution {
    public:
    string solveEquation(string equation)
    {
        string ans;
        int len=equation.size();
        int i=0;
        int x_coefficient=0,Constant=0;
        int sign=1,flag=1;//sign indicates + or -,falg indicate left or right
        for(i=0;i<len;)
        {
            sign=1;
            if(equation[i]=='=')//must the first,because after the '=',there might be a '-'
            {
                flag=-1;//all  move to the left,so the right is -
                i++;
            }
            if(equation[i]=='-')
            {
                sign=-1;
                i++;
            }
            if(equation[i]=='+')
            {
                sign=1;
                i++;
            }
    
            if(i<len&&(equation[i]>='0'&&equation[i]<='9'))
               {
                   int temp=0;//to store the coefficient
                   while(i<len&&(equation[i]>='0'&&equation[i]<='9'))
                         {
                             temp=temp*10+equation[i]-'0';
                             i++;
                         }
                    if(i<len&&equation[i]=='x')
                    {
                        x_coefficient+=(temp*sign*flag);
                        i++;
                    }
                    else
                        Constant+=(temp*sign*flag);
               }
            else//x indicate 1*x;-x indicate -1*x
            {
                x_coefficient+=(sign*flag);
                i++;
            }
        }
        if(x_coefficient==0&&Constant==0)
            ans="Infinite solutions";
        else if(x_coefficient==0)
            ans="No solution";
        else
        {
            ans="x=";
            ans+=to_string(Constant*(-1)/x_coefficient);//for all moving to the left,so,ans should multiply -1.
        }
        return ans;
    }
    };
    
  • 相关阅读:
    NYOJ 38布线问题
    NYOJ 106背包问题
    基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题
    HDOJ 2546饭卡(01背包问题)
    FBI树-数据结构(二叉树)
    二叉树遍历(flist)(二叉树,已知中序层序,求先序)
    求先序排列(二叉树已知中序和后序,求先序)
    滑雪(dp)
    Python——plot可视化数据,作业8(python programming)
    数据库SQL语言学习----左外连接,右外连接,外连接,自然连接的形象对比
  • 原文地址:https://www.cnblogs.com/qiulinzhang/p/9514390.html
Copyright © 2020-2023  润新知