結果

問題 No.222 引き算と足し算
ユーザー shinwisteria
提出日時 2017-04-02 18:50:35
言語 Java
(openjdk 23)
結果
AC  
実行時間 127 ms / 1,000 ms
コード長 815 bytes
コンパイル時間 3,326 ms
コンパイル使用メモリ 75,600 KB
実行使用メモリ 41,688 KB
最終ジャッジ日時 2024-07-08 00:11:45
合計ジャッジ時間 8,561 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
public class Tashihiki {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner s = new Scanner(System.in);
		StringBuilder str = new StringBuilder(s.nextLine());
		s.close();
		int sum = 0;
		int lp = str.lastIndexOf("+"),lm = str.lastIndexOf("-");
		int b = Integer.parseInt(str.substring(Math.max(lp, lm), str.length()));
		str.delete(Math.max(lm, lp), str.length());
		lp = str.lastIndexOf("+");
		lm = str.lastIndexOf("-");
		if(lp <= 0 && lm <= 0){  //要修正
			sum = Integer.parseInt(str.toString()) - b;
		}else{
			int a = Integer.parseInt(str.substring(0, Math.max(lm, lp)));
			str.delete(0, Math.max(lp, lm));
			if(str.charAt(0) == '+'){
				sum = a-b;
			}else{
				sum = a+b;
			}
		}
		System.out.println(sum);
	}

}
0