結果

問題 No.708 (+ー)の式
ユーザー GrenacheGrenache
提出日時 2018-09-17 10:14:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 1,883 bytes
コンパイル時間 4,489 ms
コンパイル使用メモリ 76,152 KB
実行使用メモリ 56,044 KB
最終ジャッジ日時 2023-09-25 09:17:50
合計ジャッジ時間 6,789 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,396 KB
testcase_01 AC 118 ms
55,428 KB
testcase_02 AC 118 ms
55,728 KB
testcase_03 AC 119 ms
55,896 KB
testcase_04 AC 117 ms
55,696 KB
testcase_05 AC 123 ms
55,568 KB
testcase_06 AC 125 ms
55,416 KB
testcase_07 AC 122 ms
55,780 KB
testcase_08 AC 118 ms
55,560 KB
testcase_09 AC 119 ms
55,764 KB
testcase_10 AC 118 ms
55,952 KB
testcase_11 AC 117 ms
55,728 KB
testcase_12 AC 117 ms
55,788 KB
testcase_13 AC 118 ms
56,044 KB
testcase_14 AC 123 ms
55,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class Main_yukicoder708 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		char[] s = sc.next().toCharArray();
		int n = s.length;

		Deque<String> q = new ArrayDeque<>();
		q.addLast("0");
		q.addLast("+");
		for (int i = 0; i < n; i++) {
			char c = s[i];
			
			if (c >= '0' && c <= '9') {
				int num = c - '0';
				if (q.peekLast().equals("+")) {
					q.removeLast();
					int tmp = Integer.parseInt(q.removeLast());
					tmp += num;
					q.addLast(Integer.toString(tmp));
				} else if (q.peekLast().equals("-")) {
					q.removeLast();
					int tmp = Integer.parseInt(q.removeLast());
					tmp -= num;
					q.addLast(Integer.toString(tmp));
				} else if (q.peekLast().equals("(")) {
					q.addLast(Integer.toString(num));
				}
			} else if (c == '+') {
				q.addLast("+");
			} else if (c == '-') {
				q.addLast("-");
			} else if (c == '(') {
				q.addLast("(");
			} else if (c == ')') {
				int num = Integer.parseInt(q.removeLast());
				q.removeLast(); // '('
				if (q.peekLast().equals("+")) {
					q.removeLast();
					int tmp = Integer.parseInt(q.removeLast());
					tmp += num;
					q.addLast(Integer.toString(tmp));
				} else if (q.peekLast().equals("-")) {
					q.removeLast();
					int tmp = Integer.parseInt(q.removeLast());
					tmp -= num;
					q.addLast(Integer.toString(tmp));
				}
			}
		}
		
		pr.println(Integer.parseInt(q.peekLast()));
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(INPUT == null ? System.in : new ByteArrayInputStream(INPUT.getBytes()));
		pr = new Printer(System.out);

		solve();

//		pr.close();
		pr.flush();
//		sc.close();
	}

	static String INPUT = null;

	private static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}
0