結果

問題 No.49 算数の宿題
ユーザー samplelpmas
提出日時 2017-02-02 18:19:46
言語 Java
(openjdk 23)
結果
AC  
実行時間 137 ms / 5,000 ms
コード長 1,046 bytes
コンパイル時間 2,509 ms
コンパイル使用メモリ 74,280 KB
実行使用メモリ 41,264 KB
最終ジャッジ日時 2024-12-23 01:55:08
合計ジャッジ時間 4,341 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

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