結果

問題 No.49 算数の宿題
ユーザー tsunabittsunabit
提出日時 2019-09-27 06:40:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 181 ms / 5,000 ms
コード長 994 bytes
コンパイル時間 4,080 ms
コンパイル使用メモリ 76,544 KB
実行使用メモリ 57,312 KB
最終ジャッジ日時 2023-08-24 17:43:58
合計ジャッジ時間 5,604 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 154 ms
56,900 KB
testcase_01 AC 152 ms
56,776 KB
testcase_02 AC 171 ms
56,884 KB
testcase_03 AC 181 ms
56,904 KB
testcase_04 AC 154 ms
56,912 KB
testcase_05 AC 152 ms
56,604 KB
testcase_06 AC 153 ms
56,748 KB
testcase_07 AC 151 ms
56,792 KB
testcase_08 AC 153 ms
56,816 KB
testcase_09 AC 155 ms
57,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.math.*;

public class No49 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		// indexはなかったら-1が帰る
		int index = 99;
		if(s.indexOf("*") > -1) index = s.indexOf("*");
		if(s.indexOf("+") > -1 && s.indexOf("+") < index) index = s.indexOf("+");

		String t = s.substring(0, index);
		int total = Integer.parseInt(t);
		char k = s.charAt(index);
		
		String temp = "";
		for(int i = index+1; i < s.length(); i++) {
			if(s.charAt(i) == '+' || s.charAt(i) == '*') {
				// 計算する
				int nikou = Integer.parseInt(temp);
				if(k == '+') {
					total = total * nikou;
				}else {
					total = total + nikou;
				}
				temp = "";
				k = s.charAt(i);
			}else {
				temp = temp + s.charAt(i);
			}
		}
		// 最後を計算する
		int nikou = Integer.parseInt(temp);
		if(k == '+') total = total * nikou;
		else total = total + nikou;

		System.out.println(total);
	}
}
0