結果

問題 No.708 (+ー)の式
ユーザー takeya_okinotakeya_okino
提出日時 2019-09-29 12:29:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 1,072 bytes
コンパイル時間 2,429 ms
コンパイル使用メモリ 77,840 KB
実行使用メモリ 54,544 KB
最終ジャッジ日時 2024-04-14 07:13:38
合計ジャッジ時間 5,586 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
54,084 KB
testcase_01 AC 131 ms
53,896 KB
testcase_02 AC 126 ms
54,204 KB
testcase_03 AC 125 ms
54,120 KB
testcase_04 AC 127 ms
53,812 KB
testcase_05 AC 126 ms
53,880 KB
testcase_06 AC 128 ms
53,812 KB
testcase_07 AC 131 ms
54,096 KB
testcase_08 AC 129 ms
53,712 KB
testcase_09 AC 127 ms
54,544 KB
testcase_10 AC 127 ms
53,960 KB
testcase_11 AC 127 ms
54,104 KB
testcase_12 AC 129 ms
54,160 KB
testcase_13 AC 128 ms
54,148 KB
testcase_14 AC 128 ms
54,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s = sc.next();
    int ans = 0;
    int p1 = 0;
    int p2 = 0;
    int temp = 0;
    int ti = 0;
    for(int i = 0; i < s.length(); i++) {
      if(s.charAt(i) == '(') {
        ti = 1;
      } else if(s.charAt(i) == ')') {
        if(p1 == 0) {
          ans += temp;
        } else {
          ans -= temp;
        }
        temp = 0;
        ti = 0;
        p2 = 0;
      } else if(s.charAt(i) == '+') {
        if(ti == 0) {
          p1 = 0;
        } else {
          p2 = 0;
        }        
      } else if(s.charAt(i) == '-') {
        if(ti == 0) {
          p1 = 1;
        } else {
          p2 = 1;
        }       
      } else {
        int d = Integer.parseInt(String.valueOf(s.charAt(i)));
        if(ti == 0) {
          if(p1 == 0) ans += d;
          if(p1 == 1) ans -= d;
        } else {
          if(p2 == 0) temp += d;
          if(p2 == 1) temp -= d;
        }
      }
    }
    System.out.println(ans);
  }
}
0