結果

問題 No.222 引き算と足し算
ユーザー takeya_okino
提出日時 2019-09-14 15:37:15
言語 Java
(openjdk 23)
結果
AC  
実行時間 109 ms / 1,000 ms
コード長 901 bytes
コンパイル時間 1,966 ms
コンパイル使用メモリ 77,532 KB
実行使用メモリ 41,204 KB
最終ジャッジ日時 2024-07-06 03:42:23
合計ジャッジ時間 6,842 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

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