zoukankan      html  css  js  c++  java
  • [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;
    }
    };
    
  • 相关阅读:
    团队法则100条
    How To Install and Use Redis
    李元芳履职梗概
    免费私有gitLab服务推荐
    Tornado、Bottle以及Flask
    Top Open Source Projects to Watch in 2017
    openmediavault 4.1.3 插件开发
    前端面试绝对会考的JS问题!【已经开源】
    使用window.open打开新窗口被浏览器拦截的解决方案
    微信小程序的坑之wx.miniProgram.postMessage
  • 原文地址:https://www.cnblogs.com/qiulinzhang/p/9514390.html
Copyright © 2011-2022 走看看