题解 | #四则运算#

四则运算

https://www.nowcoder.com/practice/9999764a61484d819056f807d2a91f1e

天晓得我经历了什么。。。没有算法基础,不懂那些,就是硬杠。。。懒得调整了,搞了快半天,不优化不抽取了。。
思想就是从当前输入串里直接搜索括号,搜第一个)和以这个)为结尾的子串的最后一个(,这样每次都是先从()算起,然后将结果扔到字符串里,重复循环就行。比如(a+b*(c+d)+f),首先搜索第一个),在d后面这个位置,然后截取字符串(a+b*(c+d,在这个新串里搜索最后一个(,这样得到了起始索引,然后遍历起始索引里的字符c+d,然后就是这趟循环里已经没有括号了,可以直接计算了,就不赘述了,计算完后得到一个数值,将该数值插入到起始位置对应的原始字符串里,就相当于把先算的()里的值给替换进去了,然后重复循环,直到字符串里没有操作符

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String a = str.replaceAll("\\[", "(")
                   .replaceAll("\\{", "(")
                   .replaceAll("\\]", ")")
                   .replaceAll("\\}", ")");
        StringBuilder sb = new StringBuilder(a);
        String b = sb.toString();
        //a+((b-c)*(d-b)-a)* e
        //a+a*(b+c/(a-b*(c-(d/a-1))))
        while (sb.length() > 0) {
            int endIndex = b.indexOf(")");
            int fromIndex = -1;
            if (endIndex != -1) {
                fromIndex = b.substring(0, endIndex + 1).lastIndexOf("(");
            } else {
                fromIndex = b.lastIndexOf("(");
            }
            StringBuilder num = new StringBuilder();
            char op = ' ';
            Stack<String> s = new Stack<>();
            if (fromIndex == -1 || endIndex == -1) {
                fromIndex = -1;
                endIndex = sb.length();
            }
            for (int i = fromIndex + 1; i < endIndex; i++) {
                char c = b.charAt(i);
                if (i == fromIndex + 1 && c == '-') {
                    num.append(c);
                } else if (Character.isDigit(c) && op == ' ') {
                    num.append(c);
                } else if (Character.isDigit(c) && op != ' ') {
                    num.append(c);
                    if (i == endIndex - 1) {
                        s.push(num.toString());
                        num = new StringBuilder();
                    }
                } else {
                    if (c == '-' && (b.charAt(i - 1) == '-' || b.charAt(i - 1) == '+' ||
                                     b.charAt(i - 1) == '*' || b.charAt(i - 1) == '/')) {

                        num.append(c);
                    } else {
                        s.push(num.toString());
                        num = new StringBuilder();
                        op = c;
                        s.push(String.valueOf(op));
                    }
                }
                //a + b * c
            }
            if (op == ' ') {
                System.out.println(b);
                return;
            }
            int result = cal(s);
            if (fromIndex == -1) {
                System.out.println(result);
                return;
            }
            sb.replace(fromIndex, endIndex + 1, "");
            sb.insert(fromIndex, result);
            b = sb.toString();
        }

    }

    static int cal(Stack<String> s) {
        //a + b * c *d + c
        String lft = null;
        String op = null;
        String rft = null;

        while (s.size() > 0) {
            String s1 = s.get(0);
            if (lft == null) {
                lft = s.remove(0);
            } else if (op == null) {
                op = s.remove(0);
            } else if (rft == null) {
                rft = s.remove(0);
                if (op.equals("*")) {
                    s.add(0, String.valueOf(Integer.parseInt(lft) * Integer.parseInt(rft)));
                    lft = null;
                    rft = null;
                    op = null;
                    if (s.size() == 1) {
                        break;
                    }
                } else if (op.equals("/")) {
                    s.add(0, String.valueOf(Integer.parseInt(lft) / Integer.parseInt(rft)));
                    lft = null;
                    rft = null;
                    op = null;
                    if (s.size() == 1) {
                        break;
                    }
                } else if (op.equals("+") || op.equals("-")) {
                    if (0 == s.size()) {
                        if (op.equals("+")) {
                            s.add(0, String.valueOf(Integer.parseInt(lft) + Integer.parseInt(rft)));
                        } else {
                            s.add(0, String.valueOf(Integer.parseInt(lft) - Integer.parseInt(rft)));
                        }
                        break;
                    } else {
                        String nextOp = s.get(0);
                        if (nextOp.equals("*")) {
                            nextOp = s.remove(0);
                            String nextRft = s.remove(0);
                            rft = String.valueOf(Integer.parseInt(rft) * Integer.parseInt(nextRft));
                            s.add(0, rft);
                            rft = null;
                        } else if (nextOp.equals("/")) {
                            nextOp = s.remove(0);
                            String nextRft = s.remove(0);
                            rft = String.valueOf(Integer.parseInt(rft) / Integer.parseInt(nextRft));
                            s.add(0, rft);
                            rft = null;
                        } else if (nextOp.equals("+") || nextOp.equals("-")) {
                            if (op.equals("+")) {
                                s.add(0, String.valueOf(Integer.parseInt(lft) + Integer.parseInt(rft)));
                            } else {
                                s.add(0, String.valueOf(Integer.parseInt(lft) - Integer.parseInt(rft)));
                            }

                            lft = null;
                            rft = null;
                            op = null;
                            if (s.size() == 1) {
                                break;
                            }
                        }
                    }

                }
            }
        }
        return Integer.parseInt(s.get(0));
    }
}

全部评论

相关推荐

11-01 08:48
门头沟学院 C++
伤心的候选人在吵架:佬你不要的,能不能拿户口本证明过户给我。。球球了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务