結果

問題 No.49 算数の宿題
ユーザー samplelpmassamplelpmas
提出日時 2017-02-02 18:19:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 116 ms / 5,000 ms
コード長 1,046 bytes
コンパイル時間 1,927 ms
コンパイル使用メモリ 71,944 KB
実行使用メモリ 58,040 KB
最終ジャッジ日時 2023-08-24 17:35:51
合計ジャッジ時間 3,823 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
55,980 KB
testcase_01 AC 114 ms
55,832 KB
testcase_02 AC 115 ms
58,040 KB
testcase_03 AC 116 ms
56,244 KB
testcase_04 AC 114 ms
55,872 KB
testcase_05 AC 115 ms
55,852 KB
testcase_06 AC 114 ms
55,712 KB
testcase_07 AC 113 ms
55,728 KB
testcase_08 AC 114 ms
55,672 KB
testcase_09 AC 114 ms
56,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String input = sc.next();

        int operand = 0;
        int answer = 0;
        char operation = ' ';

        for (int i = 0; i < input.length(); i++) {

            char c = input.charAt(i);

            if ('0' <= c && c <= '9') {

                operand = operand * 10 + c - '0';

            } else if (c == '*'  || c == '+') {

                if (operation == '*' || operation == ' ') {

                    answer += operand;
                    operand = 0;
                    operation = c;

                } else if (operation == '+') {

                    answer *= operand;
                    operand = 0;
                    operation = c;

                }

            }

        }

        if (operation == '*') {
            answer += operand;
        } else if (operation == '+'){
            answer *= operand;
        }

        System.out.println(answer);

    }

}
0