結果
| 問題 | No.289 数字を全て足そう |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-05-25 19:58:01 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 76 ms / 1,000 ms |
| コード長 | 2,450 bytes |
| 記録 | |
| コンパイル時間 | 3,536 ms |
| コンパイル使用メモリ | 77,888 KB |
| 実行使用メモリ | 51,164 KB |
| 最終ジャッジ日時 | 2024-09-19 19:15:44 |
| 合計ジャッジ時間 | 5,526 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 |
ソースコード
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;
}
}