結果

問題 No.289 数字を全て足そう
ユーザー nihi9119nihi9119
提出日時 2017-05-25 19:58:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 85 ms / 1,000 ms
コード長 2,450 bytes
コンパイル時間 3,843 ms
コンパイル使用メモリ 78,200 KB
実行使用メモリ 54,416 KB
最終ジャッジ日時 2023-10-19 23:20:18
合計ジャッジ時間 7,105 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
53,720 KB
testcase_01 AC 62 ms
53,720 KB
testcase_02 AC 62 ms
53,704 KB
testcase_03 AC 60 ms
53,716 KB
testcase_04 AC 61 ms
53,728 KB
testcase_05 AC 71 ms
53,736 KB
testcase_06 AC 61 ms
53,728 KB
testcase_07 AC 80 ms
54,296 KB
testcase_08 AC 81 ms
54,292 KB
testcase_09 AC 75 ms
53,740 KB
testcase_10 AC 65 ms
54,300 KB
testcase_11 AC 80 ms
54,292 KB
testcase_12 AC 75 ms
54,416 KB
testcase_13 AC 78 ms
53,768 KB
testcase_14 AC 83 ms
54,312 KB
testcase_15 AC 81 ms
54,172 KB
testcase_16 AC 70 ms
54,304 KB
testcase_17 AC 77 ms
53,736 KB
testcase_18 AC 81 ms
54,168 KB
testcase_19 AC 80 ms
54,288 KB
testcase_20 AC 78 ms
54,296 KB
testcase_21 AC 60 ms
53,728 KB
testcase_22 AC 80 ms
52,276 KB
testcase_23 AC 85 ms
54,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package test;

/*No.289 数字を全て足そう
*
* 文字列SSが与えられるので, その中のそれぞれの数字を1桁の数値とみなして, 全ての合計値を求めてください.
* 例えば1test23という文字列の数字の合計値は1+2+3=61+2+3=6となる.
* */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Question_03_0515_1 {

	//桁数チェックで使用
	static final int MIN_LENGTH = 1;
	static final int MAX_LENGTH = 10000;

	// inputCheckで
	static final int NOMAL = 0;
	static final int ERROR_NUMBER_LENGTH = 1;
	static final int ERROR_CHARACTER = 2;

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

		try {
			String inputString = br.readLine();
			// 入力チェック
			if (inputCheck(inputString) == NOMAL) {
				int result = 0;
				char[] charArry = inputString.toCharArray();
				for (int i = 0; i < charArry.length; i++) {
					boolean isDigit = Character.isDigit(charArry[i]);
					if (isDigit) {
						result += Character.getNumericValue(charArry[i]);
					}
				}
				System.out.println(result);
			} else {
				if (inputCheck(inputString) == ERROR_NUMBER_LENGTH) {
					System.out.println("桁数が正しくありません");
				} else {
					System.out.println("英数半角以外の文字が入力されています");
				}
			}

		} catch (IOException e) {
			System.out.println("エラーが発生しました");
		} finally {
			try {
				re.close();
				br.close();
			} catch (IOException e) {
				System.out.println("InputStreamReader、BufferedReaderクローズ中にエラーが発生しました");
			}
		}
	}

	/*
	 * 入力値が正しいか判定
	 * 返り値:
	 * NOMAL=問題なし
	 * ERROR_NUMBER_LENGTH =桁数に問題あり、
	 * ERROR_CHARACTER=半角英数字以外あり
	 */
	private static int inputCheck(String inputString) {
		int result = NOMAL;

		// 判定するパターン生成
		Pattern p = Pattern.compile("^[0-9a-zA-Z]+$");

		// 桁数チェック
		if (inputString.length() < MIN_LENGTH || inputString.length() > MAX_LENGTH) {
			result = ERROR_NUMBER_LENGTH;
		}
		// 英数半角チェック
		else {
			Matcher m = p.matcher(inputString);
			if (!m.find()) {
				result = ERROR_CHARACTER;
			}
		}
		return result;
	}

}
0