首页 > 猿辅导笔试7.31第二题
头像
牛客673434934号
编辑于 2021-08-01 09:34
+ 关注

猿辅导笔试7.31第二题

猿辅导笔试7.31第二题
自测都能过,提交0%

//
//修改过后
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String boxs = sc.nextLine();
        int n = boxs.length();
        int ret = 0;
        Stack<Integer> c = new Stack<Integer> ();
        c.push(0);
        for (int i = 0; i < n; ++i) {
            // System.out.println(c.toString());
            char ch = boxs.charAt(i);
            if (ch == '[') {
                c.push(1);
            } else if (ch == ']') {
                if (i < n-1 && (Character.isDigit(boxs.charAt(i+1)))) {
                    c.push(c.pop() * (boxs.charAt(i+1) - '0'));
                    i = i+1;
                }
                c.push(c.pop() + c.pop());
            }
        }
        while(!c.empty())
            ret += c.pop();
        System.out.println(ret);
    }
}
// 原不正确代码:



import java.util.Scanner; import java.util.Stack; public class Main {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         String boxs = sc.nextLine();         int n = boxs.length();         int ret = 0;         Stack<Integer> c = new Stack<Integer> ();         Stack<Character> s = new Stack<> ();         c.push(0);         s.push(boxs.charAt(0));         for (int i = 1; i < n; ++i) {             // System.out.println(c.toString());             char ch = boxs.charAt(i);             if (ch == '[') {                 if ( !s.empty() && s.peek() == ']') {                     s.pop();                     s.pop();                     if (!c.empty()) {                         if (c.peek().equals(0)) {                             c.pop();                             c.push(1);                         } else {                             int sum = 0;                             int e;                             while (c.peek() != 0) {                                 e = c.pop();                                 sum += e;                             }                             c.pop();                             c.push(sum + 1);                         }                     }                 }                 c.push(0);                 s.push(ch);             } else if (ch == ']') {                 s.push(ch);             } else {                 if ( !s.empty()) {                     s.pop();                     s.pop();                     if (!c.empty()) {                         if (c.peek().equals(0)) {                             c.pop();                             c.push(ch - '0');                         } else {                             int sum = 0;                             int e;                             while (c.peek() != 0) {                                 e = c.pop();                                 sum += e;                             }                                                          c.pop();                             c.push((sum+1)* (ch - '0'));                         }                     }                 }             }         }         while(!c.empty())              ret += c.pop();         System.out.println(ret);     } }


全部评论

(6) 回帖
加载中...
话题 回帖

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐