結果

問題 No.193 筒の数式
ユーザー tentententen
提出日時 2022-10-13 10:26:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 49 ms / 1,000 ms
コード長 2,266 bytes
コンパイル時間 2,269 ms
コンパイル使用メモリ 74,688 KB
実行使用メモリ 49,756 KB
最終ジャッジ日時 2023-09-08 18:59:12
合計ジャッジ時間 4,280 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
49,580 KB
testcase_01 AC 47 ms
49,696 KB
testcase_02 AC 49 ms
49,524 KB
testcase_03 AC 48 ms
49,584 KB
testcase_04 AC 47 ms
49,580 KB
testcase_05 AC 47 ms
49,652 KB
testcase_06 AC 47 ms
49,436 KB
testcase_07 AC 47 ms
49,536 KB
testcase_08 AC 47 ms
49,756 KB
testcase_09 AC 47 ms
49,568 KB
testcase_10 AC 47 ms
49,552 KB
testcase_11 AC 48 ms
49,576 KB
testcase_12 AC 47 ms
49,524 KB
testcase_13 AC 47 ms
49,460 KB
testcase_14 AC 47 ms
49,600 KB
testcase_15 AC 48 ms
49,648 KB
testcase_16 AC 48 ms
49,596 KB
testcase_17 AC 48 ms
49,696 KB
testcase_18 AC 47 ms
49,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        String s = sc.next();
        int length = s.length();
        char[] values = (s + s).toCharArray();
        long ans = Long.MIN_VALUE;
        for (int i = 0; i < length; i++) {
            if (values[i] == '+' || values[i] == '-' || values[i + length - 1] == '+' || values[i + length - 1] == '-') {
                continue;
            }
            long sum = 0;
            long tmp = 0;
            boolean isPlus = true;
            for (int j = 0; j < length; j++) {
                if (values[i + j] == '+') {
                    if (isPlus) {
                        sum += tmp;
                    } else {
                        sum -= tmp;
                        isPlus = true;
                    }
                    tmp = 0;
                } else if (values[i + j] == '-') {
                    if (isPlus) {
                        sum += tmp;
                        isPlus = false;
                    } else {
                        sum -= tmp;
                    }
                    tmp = 0;
                } else {
                    tmp *= 10;
                    tmp += values[i + j] - '0';
                }
            }
            if (isPlus) {
                sum += tmp;
            } else {
                sum -= tmp;
            }
            ans = Math.max(ans, sum);
        }
        System.out.println(ans);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0