結果

問題 No.49 算数の宿題
ユーザー takeya_okinotakeya_okino
提出日時 2019-09-14 14:10:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 122 ms / 5,000 ms
コード長 1,170 bytes
コンパイル時間 2,197 ms
コンパイル使用メモリ 76,192 KB
実行使用メモリ 56,288 KB
最終ジャッジ日時 2023-08-24 17:43:43
合計ジャッジ時間 4,108 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,396 KB
testcase_01 AC 118 ms
55,648 KB
testcase_02 AC 117 ms
55,660 KB
testcase_03 AC 118 ms
55,812 KB
testcase_04 AC 117 ms
55,992 KB
testcase_05 AC 117 ms
55,704 KB
testcase_06 AC 116 ms
55,344 KB
testcase_07 AC 117 ms
55,328 KB
testcase_08 AC 122 ms
56,288 KB
testcase_09 AC 118 ms
55,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s = sc.next();
    int ans = 0;
    String st = "";
    int p = 0;
    String c = "";
    for(int i = 0; i < s.length(); i++) {
      String str = String.valueOf(s.charAt(i));
      if(str.equals("*")) {
        int d = Integer.parseInt(st);
        st = "";
        if(p == 0) {
          ans += d;
        } else {
          if(c.equals("*")) {
            ans += d;
          } else {
            ans *= d;
          }
        }
        p++;
        c = "*";
      } else if(str.equals("+")) {
        int d = Integer.parseInt(st);
        st = "";
        if(p == 0) {
          ans += d;
        } else {
          if(c.equals("*")) {
            ans += d;
          } else {
            ans *= d;
          }          
        }
        p++;
        c = "+";
      } else {
        st += str;
        int d = Integer.parseInt(st);
        if(i == s.length() - 1) {
          if(c.equals("*")) {
            ans += d;
          } else {
            ans *= d;
          }
        }
      }
    }
    System.out.println(ans);
  }
}
0