• [每日一题] leetcode 640. 求解方程


    等号左右两边代码一样,就是+ - 不一样

    将x都移到左边 整数移到右边

    class Solution {
    public:
    
        int change(string str)
        {
            if(str == "0") return 1;
            int len = str.length();
            int ret = 0;
            for(int i = 0; i < len; i++)
            {
                ret = ret * 10 + str[i] - '0';
            }
            return ret;
        }
        string rev(int x)
        {
            if(x == 0) return "0";
            string str = "";
            while(x)
            {
                int tmp = x % 10;
                x /= 10;
                str += '0' + tmp;
            }
            reverse(str.begin(), str.end());
            return str;
        }
    
    
        string solveEquation(string equation) {
            int cnt1 = 0, cnt2 = 0, f = 0, s;
            int len = equation.length();
            string str;
            int i = 0;
            while(i < len)
            {
                s = 0;
    
                str = "0";
                if(equation[i] == '=')
                {
                    f = 1;
                    i++;
                }
                if(equation[i] == '-')
                {
                    s = 1;
                    i++;
                }
                else if(equation[i] == '+') i++;
                if(f == 1)
                {
                    while(equation[i] >= '0' && equation[i] <= '9') str += equation[i++];
                    if(equation[i] == 'x')
                    {
                        if(s == 0) cnt2 -= change(str);
                        else cnt2 += change(str);
                        i++;
                    }
                    else 
                    {
                        if(s == 0) cnt1 += change(str);
                        else cnt1 -= change(str);
                    }
                }
                else
                {
                    while(equation[i] >= '0' && equation[i] <= '9') str += equation[i++];
                    if(equation[i] == 'x')
                    {
                        if(s == 0) cnt2 += change(str);
                        else cnt2 -= change(str);
                        i++;
                    }
                    else 
                    {
                   //     cout << str << endl;
                        if(s == 0) cnt1 -= change(str);
                        else cnt1 += change(str);
                    }
                }
    
    
            }
           // cout << cnt2 << "  " << cnt1 << endl;
            if(cnt2 == 0)
                if(cnt1 == 0) str = "Infinite solutions";
                else str = "No solution";
            else
            {
                str = "x=";
                int tmp = cnt1 / cnt2;
                if(tmp < 0) str += '-', tmp = -tmp;
              // cout << tmp << endl;
                str += rev(tmp);
            }
            return str;
    
    
    
    
    
    
        }
    };
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    Button 使用Command 按钮置灰未更新
    C# TextBox 焦点
    MultiTigger 绑定异常处理
    C# 获取程序路径
    Linux 权限设置chmod
    WPF SpreadSheetGear电子表单
    WPF 窗口
    Excel公式 提取文件路径后缀
    C#/VB.NET 获取电脑属性(硬盘ID、硬盘容量、Cpu序列号、MAC地址、系统类型)
    DevExpress Carousel 设置水平滑动列表
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/14741376.html
Copyright © 2020-2023  润新知