結果

問題 No.708 (+ー)の式
ユーザー tenten
提出日時 2020-11-18 11:54:04
言語 Java
(openjdk 23)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 1,702 bytes
コンパイル時間 2,376 ms
コンパイル使用メモリ 78,940 KB
実行使用メモリ 54,260 KB
最終ジャッジ日時 2024-07-23 08:54:26
合計ジャッジ時間 5,168 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

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);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0