結果
問題 | No.49 算数の宿題 |
ユーザー |
|
提出日時 | 2019-09-27 06:40:14 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 175 ms / 5,000 ms |
コード長 | 994 bytes |
コンパイル時間 | 4,508 ms |
コンパイル使用メモリ | 79,284 KB |
実行使用メモリ | 42,400 KB |
最終ジャッジ日時 | 2024-12-23 02:04:54 |
合計ジャッジ時間 | 6,389 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 |
ソースコード
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); } }