結果

問題 No.222 引き算と足し算
ユーザー nihi9119nihi9119
提出日時 2017-06-13 20:29:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 55 ms / 1,000 ms
コード長 3,052 bytes
コンパイル時間 3,500 ms
コンパイル使用メモリ 77,912 KB
実行使用メモリ 53,492 KB
最終ジャッジ日時 2023-10-24 21:50:08
合計ジャッジ時間 6,399 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
53,468 KB
testcase_01 AC 52 ms
53,480 KB
testcase_02 AC 51 ms
51,512 KB
testcase_03 AC 51 ms
53,468 KB
testcase_04 AC 53 ms
53,488 KB
testcase_05 AC 53 ms
53,476 KB
testcase_06 AC 52 ms
53,488 KB
testcase_07 AC 53 ms
53,472 KB
testcase_08 AC 53 ms
53,468 KB
testcase_09 AC 52 ms
53,476 KB
testcase_10 AC 51 ms
53,480 KB
testcase_11 AC 53 ms
53,468 KB
testcase_12 AC 53 ms
53,484 KB
testcase_13 AC 52 ms
52,420 KB
testcase_14 AC 52 ms
53,468 KB
testcase_15 AC 51 ms
53,476 KB
testcase_16 AC 53 ms
53,468 KB
testcase_17 AC 52 ms
53,476 KB
testcase_18 AC 53 ms
53,472 KB
testcase_19 AC 53 ms
53,484 KB
testcase_20 AC 54 ms
53,488 KB
testcase_21 AC 53 ms
53,472 KB
testcase_22 AC 55 ms
53,472 KB
testcase_23 AC 54 ms
53,476 KB
testcase_24 AC 53 ms
53,476 KB
testcase_25 AC 54 ms
53,492 KB
testcase_26 AC 53 ms
53,472 KB
testcase_27 AC 52 ms
53,472 KB
testcase_28 AC 52 ms
53,472 KB
testcase_29 AC 52 ms
53,472 KB
testcase_30 AC 52 ms
53,480 KB
testcase_31 AC 53 ms
53,476 KB
testcase_32 AC 53 ms
53,476 KB
testcase_33 AC 55 ms
53,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package test7;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * No.222 引き算と足し算
 * http://yukicoder.me/problems/no/222
 */

public class Question_20_0613 {

	final static int X_MIN = -10000;
	final static int Y_MIN = -10000;
	final static int X_MAX = 10000;
	final static int Y_MAX = 10000;

	public static void main(String[] args) {
		InputStreamReader re = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(re);

		try {
			String inputString = br.readLine();
			int plusPlace = 0;
			int minusPlace = 0;
			int signPlace = 0;
			String sign = "+";
			int result = 0;

			//算術演算子を探し、場所と符号(ひっくり返したもの)を記録
			if (inputString.indexOf("+", 1) > 0){
				plusPlace = inputString.indexOf("+", 1);
			}
			if (inputString.indexOf("-", 1) > 0) {
				minusPlace = inputString.indexOf("-", 1);
			}

			//数値が0なら大きいほうを、正なら早い方を入れる
			if (plusPlace == 0 || minusPlace == 0) {
				signPlace = Math.max(plusPlace, minusPlace);
				if (minusPlace == 0) {
					sign = "-";
				}
			} else {
				signPlace = Math.min(plusPlace, minusPlace);
				if (Math.min(plusPlace, minusPlace) == plusPlace) {
					sign = "-";
				}
			}

			//数値変換(符号考慮)
			int numX = resultNum(inputString.substring(0, signPlace));
			int numY = resultNum(inputString.substring(signPlace + 1, inputString.length()));

			//数値チェック
			if (NumJudg(numX, X_MIN, X_MAX) && NumJudg(numY, Y_MIN, Y_MAX)) {
				//計算
				if (sign.equals("+")) {
					result = numX + numY;
				} else {
					result = numX - numY;
				}
				System.out.println(result);
			} else {
				System.out.println("数値が有効範囲外です");
			}
		} catch (NumberFormatException e){
			System.out.println("数字を入力して下さい");
		} catch (IOException e) {
			System.out.println("エラーが発生しました");
		} finally {
			try {
				re.close();
				br.close();
			} catch (IOException e) {
				System.out.println("InputStreamReader、BufferedReaderクローズ中にエラーが発生しました");
			}
		}
	}

	/**
	 * 有効値判定
	 * @param input 判定するもの
	 * @param max 最大値
	 * @param min 最小値
	 * @return 範囲内ならtrue,範囲外ならfalseを返す
	 */
	private static boolean NumJudg(int input, int min, int max) {
		Boolean result = false;
		if (min <= input && input <= max) {
			result = true;
		}
		return result;
	}

	/**
	 * 数値に変換して返す(符号も考慮)
	 * @param str
	 * @return 変換した数値
	 */
	private static int resultNum(String str) {
		int result = 0;
		if (str.substring(0, 1).equals("-")) {
			String str1 = str.substring(1, str.length());
			int num = Integer.parseInt(str1);
			result = -num;
		} else if (str.substring(0, 1).equals("+")) {
			String str1 = str.substring(1, str.length());
			result = Integer.parseInt(str1);
		} else {
			result = Integer.parseInt(str);
		}
		return result;
	}
}
0