結果

問題 No.708 (+ー)の式
ユーザー tentententen
提出日時 2020-11-18 11:54:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 1,702 bytes
コンパイル時間 2,073 ms
コンパイル使用メモリ 74,676 KB
実行使用メモリ 56,096 KB
最終ジャッジ日時 2023-09-30 14:53:51
合計ジャッジ時間 5,031 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
55,608 KB
testcase_01 AC 118 ms
55,772 KB
testcase_02 AC 118 ms
55,668 KB
testcase_03 AC 118 ms
55,668 KB
testcase_04 AC 121 ms
55,632 KB
testcase_05 AC 119 ms
55,860 KB
testcase_06 AC 119 ms
55,780 KB
testcase_07 AC 119 ms
55,568 KB
testcase_08 AC 118 ms
55,676 KB
testcase_09 AC 119 ms
55,748 KB
testcase_10 AC 120 ms
55,324 KB
testcase_11 AC 122 ms
55,700 KB
testcase_12 AC 121 ms
55,628 KB
testcase_13 AC 121 ms
56,096 KB
testcase_14 AC 122 ms
55,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] arr = sc.next().toCharArray();
        ArrayDeque<Integer> numStack = new ArrayDeque<>();
        ArrayDeque<Boolean> boolStack = new ArrayDeque<>();
        numStack.push(0);
        boolStack.push(true);
        int num = 0;
        int tmp = 0;
        boolean isPlus = true;
        for (char c : arr) {
            if (c == '+' || c == '-') {
                if (isPlus) {
                    num += tmp;
                } else {
                    num -= tmp;
                }
                tmp = 0;
                isPlus = (c == '+');
            } else if (c == '(') {
                if (isPlus) {
                    num += tmp;
                } else {
                    num -= tmp;
                }
                numStack.push(num);
                boolStack.push(isPlus);
                num = 0;
                tmp = 0;
                isPlus = true;
            } else if (c == ')') {
                if (isPlus) {
                    num += tmp;
                } else {
                    num -= tmp;
                }
                isPlus = boolStack.pop();
                if (isPlus) {
                    num = numStack.pop() + num;
                } else {
                    num = numStack.pop() - num;
                }
                tmp = 0;
                isPlus = true;
            } else {
                tmp *= 10;
                tmp += c - '0';
            }
        }
        if (isPlus) {
            num += tmp;
        } else {
            num -= tmp;
        }
        System.out.println(num);
    }
}
0