結果

問題 No.708 (+ー)の式
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2018-06-29 23:15:51
言語 Java19
(openjdk 21)
結果
RE  
実行時間 -
コード長 1,790 bytes
コンパイル時間 2,156 ms
コンパイル使用メモリ 76,256 KB
実行使用メモリ 57,172 KB
最終ジャッジ日時 2023-09-13 15:38:59
合計ジャッジ時間 5,335 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
56,096 KB
testcase_01 AC 156 ms
57,064 KB
testcase_02 AC 157 ms
56,800 KB
testcase_03 AC 120 ms
55,832 KB
testcase_04 AC 157 ms
57,172 KB
testcase_05 AC 157 ms
55,220 KB
testcase_06 AC 157 ms
56,828 KB
testcase_07 AC 157 ms
57,028 KB
testcase_08 AC 158 ms
56,912 KB
testcase_09 AC 158 ms
56,904 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 164 ms
56,740 KB
testcase_13 AC 159 ms
56,788 KB
testcase_14 AC 124 ms
56,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        String ans = calculate(s);
        System.out.println(ans);
    }
    
    static private String calculate(String s) {
        if( !s.contains("+") && !s.contains("-") || s.indexOf("-") == 0 ) {
            return s;
        }
        
        if( s.contains("(") ) {
            int start = s.indexOf("(");
            int end = s.indexOf(")");
            String left = s.substring(0, start);
            String center = s.substring(start+1, end);
            String right = s.substring(end+1, s.length());
            String sNew = left + calculate(center) + right;
            return calculate(sNew);
        } else {
            for(int i = 0; i < s.length(); i++) {
                if(s.charAt(i) == '+' || s.charAt(i) == '-') {
                    int j = i+1;
                    for( ; j < s.length(); j++) {
                        if(s.charAt(j) == '+' || s.charAt(j) == '-') {
                            break;
                        }
                    }
                    String a = s.substring(0,i);
                    String b = s.substring(i+1,j);
                    String sign = s.substring(i,i+1);
                    return calculate (plusminus(a,b,sign) + s.substring(j,s.length()));
                }
            }
        }
        return "";
    }
    
    static private String plusminus(String a, String b, String sign) {
        int aint = Integer.parseInt(a);
        int bint = Integer.parseInt(b);
        int rint;
        if("+".equals(sign)) {
            rint = aint + bint;
        } else {
            rint = aint - bint;
        }
        return String.valueOf(rint);
    }
}
0