結果

問題 No.708 (+ー)の式
ユーザー Pump0129Pump0129
提出日時 2018-07-23 03:27:24
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 1,745 bytes
コンパイル時間 2,444 ms
コンパイル使用メモリ 73,876 KB
実行使用メモリ 56,228 KB
最終ジャッジ日時 2023-08-27 01:07:47
合計ジャッジ時間 5,376 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
55,764 KB
testcase_01 AC 120 ms
55,376 KB
testcase_02 AC 117 ms
55,928 KB
testcase_03 AC 117 ms
56,068 KB
testcase_04 AC 121 ms
55,672 KB
testcase_05 AC 120 ms
55,836 KB
testcase_06 AC 119 ms
55,664 KB
testcase_07 AC 121 ms
55,964 KB
testcase_08 AC 121 ms
56,024 KB
testcase_09 RE -
testcase_10 AC 118 ms
55,684 KB
testcase_11 AC 119 ms
56,160 KB
testcase_12 RE -
testcase_13 AC 119 ms
55,932 KB
testcase_14 AC 119 ms
56,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package net.ipipip0129.yukicoder.no708;

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 演算用クラスのインスタンスを作成
        Operation operation = new Operation(scan.nextLine());
        System.out.println(operation.getNum());
    }
}

class Operation {
    int num = 0;
    // 0=-,1=+
    int prev_sign = 0;
    Operation(String formula){
        num = Integer.parseInt(formula.substring(0, 1));
        for (int i = 1; i < formula.length(); i++) {
            String str = formula.substring(i, i+1);
            if (str.equalsIgnoreCase("-")) {
                prev_sign = 0;
            } else if (str.equalsIgnoreCase("+")) {
                prev_sign = 1;
            } else if (str.equalsIgnoreCase("(")){ // "("が出てきたら")"までの式を演算クラスで演算し答えを取得
                int end_index = 0;
                for (int j = i; !formula.substring(j, j + 1).equalsIgnoreCase(")"); j++) {
                    end_index = j;
                }
                end_index++;
                Operation operation = new Operation(formula.substring(i + 1, end_index));
                if (prev_sign == 0 ) {
                    num -= operation.num;
                } else {
                    num += operation.num;
                }
                i = end_index;
            } else {
                if (prev_sign == 0) {
                    num -= Integer.parseInt(str);
                } else {
                    num += Integer.parseInt(str);
                }
            }
        }
    }

    public int getNum() {
        return num;
    }
}
0