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;
    }
    };
    
  • 相关阅读:
    如何在 Creator3D 中切换模型贴图,超级简单!
    研究了3天,终于将 Shader 移植到 Cocos Creator 2.2.0 上了!
    Creator3D 守护你的球球—UV动画与天空盒
    一个玩游戏的失足青年,转行做软件开发的挣扎过程
    Cocos Creator 3D 打砖块教程(二) | 子弹发射与摄像机平滑移动
    Cocos Creator 3D 打砖块图文教程(一)
    Creator3D长什么样?看看官方惊艳的DEMO就知道了,附在线体验!
    不要总想着二进制
    关于 JSX
    早教
  • 原文地址:https://www.cnblogs.com/qiulinzhang/p/9514390.html
Copyright © 2011-2022 走看看