結果
問題 |
No.289 数字を全て足そう
|
ユーザー |
|
提出日時 | 2017-05-15 01:25:29 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 123 ms / 1,000 ms |
コード長 | 2,002 bytes |
コンパイル時間 | 3,944 ms |
コンパイル使用メモリ | 78,068 KB |
実行使用メモリ | 42,008 KB |
最終ジャッジ日時 | 2024-09-16 05:56:09 |
合計ジャッジ時間 | 6,835 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
/*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; } }