結果

問題 No.193 筒の数式
ユーザー mobius_bkstmobius_bkst
提出日時 2015-04-27 11:47:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 53 ms / 1,000 ms
コード長 2,455 bytes
コンパイル時間 3,854 ms
コンパイル使用メモリ 79,380 KB
実行使用メモリ 50,492 KB
最終ジャッジ日時 2023-09-18 13:47:57
合計ジャッジ時間 5,907 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
50,060 KB
testcase_01 AC 51 ms
49,980 KB
testcase_02 AC 49 ms
50,076 KB
testcase_03 AC 49 ms
49,972 KB
testcase_04 AC 48 ms
50,296 KB
testcase_05 AC 48 ms
50,016 KB
testcase_06 AC 48 ms
50,220 KB
testcase_07 AC 48 ms
50,120 KB
testcase_08 AC 51 ms
50,060 KB
testcase_09 AC 51 ms
50,264 KB
testcase_10 AC 47 ms
49,996 KB
testcase_11 AC 47 ms
50,208 KB
testcase_12 AC 50 ms
50,148 KB
testcase_13 AC 51 ms
50,492 KB
testcase_14 AC 48 ms
50,356 KB
testcase_15 AC 48 ms
50,324 KB
testcase_16 AC 47 ms
50,348 KB
testcase_17 AC 48 ms
48,432 KB
testcase_18 AC 53 ms
50,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class No193 {

    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    System.in));
            int max = Integer.MIN_VALUE;
            String S = br.readLine();

            int Slength = S.length();
            String head = "";
            String tail = "";
            for (int i = 0; i < Slength; i++) {

                head = S.substring(0, 1);
                tail = S.substring(Slength - 1, Slength);

                // 先頭としっぽが記号だったらcontinue
                if (head.equals("+") || head.equals("-") || tail.equals("+")
                        || tail.equals("-")) {
                    S = rotateString(S, 1);
                    continue;
                }

                max = Math.max(calc(S), max);

                // 円筒のきる部分を変える
                S = rotateString(S, 1);
            }

             System.out.println(max);
        } catch (Exception e) {
            System.err.println("Error:" + e.getMessage());
        }
    }

    private static int calc(String S) {
        Queue<String> operatorQueue = new LinkedList<String>();
        Queue<Integer> numQuque = new LinkedList<Integer>();

        String[] numArray = S.split("[\\+-]");

        // 数字をキューに格納
        for (int i = 0; i < numArray.length; i++) {
            numQuque.add(Integer.parseInt(numArray[i]));
        }

        int sLnegth = S.length();
        // 演算子をキューに格納
        for (int i = 0; i < sLnegth; i++) {
            if (!S.substring(i, i + 1).matches("[0-9]"))
                operatorQueue.add(S.substring(i, i + 1));
        }

        // 計算
        int ans = numQuque.poll();
        int num = 0;
        String ope = "";
        while (!numQuque.isEmpty()) {
            num = numQuque.poll();
            ope = operatorQueue.poll();
            if (ope.equals("+")) {
                ans = ans + num;
            } else {
                ans = ans - num;
            }
        }
        return ans;

    }

    static String rotateString(String S, int n) {
        StringBuilder sb = new StringBuilder(S);
        String head = sb.substring(0, n);
        sb.delete(0, n);
        sb.append(head);
        return sb.toString();
    }

}
0