結果

問題 No.49 算数の宿題
ユーザー mobius_bkstmobius_bkst
提出日時 2015-05-02 18:11:26
言語 Java21
(openjdk 21)
結果
AC  
実行時間 49 ms / 5,000 ms
コード長 1,501 bytes
コンパイル時間 3,316 ms
コンパイル使用メモリ 75,928 KB
実行使用メモリ 50,504 KB
最終ジャッジ日時 2023-08-24 17:25:34
合計ジャッジ時間 4,443 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
50,148 KB
testcase_01 AC 47 ms
50,216 KB
testcase_02 AC 46 ms
48,228 KB
testcase_03 AC 49 ms
50,400 KB
testcase_04 AC 48 ms
50,220 KB
testcase_05 AC 46 ms
50,016 KB
testcase_06 AC 46 ms
50,280 KB
testcase_07 AC 46 ms
50,504 KB
testcase_08 AC 46 ms
50,152 KB
testcase_09 AC 46 ms
50,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class No49 {

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

            System.out.println(calc(S));
        } 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;

    }

}
0