結果

問題 No.289 数字を全て足そう
ユーザー nihi9119nihi9119
提出日時 2017-05-15 01:25:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 107 ms / 1,000 ms
コード長 2,002 bytes
コンパイル時間 3,509 ms
コンパイル使用メモリ 74,236 KB
実行使用メモリ 55,848 KB
最終ジャッジ日時 2023-10-14 11:04:00
合計ジャッジ時間 6,663 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,684 KB
testcase_01 AC 46 ms
50,308 KB
testcase_02 AC 46 ms
50,132 KB
testcase_03 AC 46 ms
50,120 KB
testcase_04 AC 45 ms
48,348 KB
testcase_05 AC 80 ms
52,456 KB
testcase_06 AC 64 ms
52,812 KB
testcase_07 AC 97 ms
55,152 KB
testcase_08 AC 96 ms
55,848 KB
testcase_09 AC 69 ms
51,720 KB
testcase_10 AC 78 ms
53,064 KB
testcase_11 AC 92 ms
52,592 KB
testcase_12 AC 107 ms
55,216 KB
testcase_13 AC 90 ms
53,744 KB
testcase_14 AC 98 ms
55,160 KB
testcase_15 AC 81 ms
53,288 KB
testcase_16 AC 87 ms
53,072 KB
testcase_17 AC 84 ms
55,640 KB
testcase_18 AC 97 ms
55,084 KB
testcase_19 AC 94 ms
55,340 KB
testcase_20 AC 95 ms
55,140 KB
testcase_21 AC 46 ms
49,824 KB
testcase_22 AC 99 ms
55,680 KB
testcase_23 AC 104 ms
55,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*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 MAX_LENGTH = 1;
	static final int MIN_LENGTH = 10000;

	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) == 0) {
				int result = 0;
				Pattern p = Pattern.compile("^[0-9]+$");
				String[] inputStringArry = inputString.split("");
				for (int i = 0; i < inputStringArry.length; i++) {
					Matcher m = p.matcher(inputStringArry[i]);
					if (m.find()) {
						result += Integer.parseInt(inputStringArry[i]);
					} else {
						continue;
					}
				}
				System.out.println(result);
			} else {
				if (inputCheck(inputString) == 1) {
					System.out.println("桁数が正しくありません");
				} else {
					System.out.println("英数半角以外の文字が入力されています");
				}

			}

		} catch (IOException e) {

		} finally {

		}
	}

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

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

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

}
0