結果

問題 No.708 (+ー)の式
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2018-06-29 23:36:12
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,183 bytes
コンパイル時間 3,282 ms
コンパイル使用メモリ 76,152 KB
実行使用メモリ 58,976 KB
最終ジャッジ日時 2023-09-13 15:45:42
合計ジャッジ時間 6,631 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,720 KB
testcase_01 AC 159 ms
56,652 KB
testcase_02 AC 159 ms
56,648 KB
testcase_03 AC 121 ms
56,284 KB
testcase_04 AC 161 ms
57,228 KB
testcase_05 AC 161 ms
57,272 KB
testcase_06 AC 159 ms
56,960 KB
testcase_07 AC 161 ms
57,056 KB
testcase_08 AC 161 ms
56,896 KB
testcase_09 AC 161 ms
57,000 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 163 ms
56,852 KB
testcase_13 AC 164 ms
55,252 KB
testcase_14 AC 125 ms
55,788 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 = calculate(s.substring(start+1, end));
            String right = s.substring(end+1, s.length());
            if(center.charAt(0) == '-') {
                if(left.charAt(left.length()-1) == '+') {
                    left = left.substring(0,left.length()-1);
                } else if(left.charAt(left.length()-1) == '-') {
                    left = left.substring(0,left.length()-1) + "+";
                    center = center.substring(1,center.length());
                }
            }
            String sNew = left + 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