結果
問題 | No.49 算数の宿題 |
ユーザー |
![]() |
提出日時 | 2015-05-02 18:11:26 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 59 ms / 5,000 ms |
コード長 | 1,501 bytes |
コンパイル時間 | 3,804 ms |
コンパイル使用メモリ | 80,740 KB |
実行使用メモリ | 37,472 KB |
最終ジャッジ日時 | 2024-12-23 01:45:03 |
合計ジャッジ時間 | 4,676 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 |
ソースコード
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;}}