結果

問題 No.708 (+ー)の式
ユーザー htensaihtensai
提出日時 2020-01-18 20:22:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 1,235 bytes
コンパイル時間 2,090 ms
コンパイル使用メモリ 72,364 KB
実行使用メモリ 49,708 KB
最終ジャッジ日時 2023-09-10 11:18:25
合計ジャッジ時間 3,847 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,412 KB
testcase_01 AC 42 ms
49,104 KB
testcase_02 AC 42 ms
49,412 KB
testcase_03 AC 41 ms
49,152 KB
testcase_04 AC 41 ms
49,708 KB
testcase_05 AC 41 ms
49,252 KB
testcase_06 AC 42 ms
49,388 KB
testcase_07 AC 42 ms
49,304 KB
testcase_08 AC 42 ms
49,288 KB
testcase_09 AC 42 ms
49,300 KB
testcase_10 AC 42 ms
49,304 KB
testcase_11 AC 42 ms
49,368 KB
testcase_12 AC 41 ms
49,240 KB
testcase_13 AC 41 ms
49,348 KB
testcase_14 AC 41 ms
49,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char[] arr = br.readLine().toCharArray();
        ArrayDeque<Integer> deqInt = new ArrayDeque<>();
        ArrayDeque<Boolean> deqBool = new ArrayDeque<>();
        int base = 0;
        boolean isPlus = true;
        for (char c : arr) {
            if (c == '+') {
                isPlus = true;
            } else if (c == '-') {
                isPlus = false;
            } else if (c == '(') {
                deqInt.push(base);
                deqBool.push(isPlus);
                base = 0;
                isPlus = true;
            } else if (c == ')') {
                int x = deqInt.pop();
                isPlus = deqBool.pop();
                if (isPlus) {
                    base = x + base;
                } else {
                    base = x - base;
                }
            } else {
                if (isPlus) {
                    base += c - '0';
                } else {
                    base -= c - '0';
                }
            }
        }
        System.out.println(base);
    }
}
0