結果
問題 | No.423 ハムスター語初級(数詞) |
ユーザー |
|
提出日時 | 2017-06-05 18:21:11 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 60 ms / 2,000 ms |
コード長 | 3,390 bytes |
コンパイル時間 | 3,320 ms |
コンパイル使用メモリ | 83,800 KB |
実行使用メモリ | 50,724 KB |
最終ジャッジ日時 | 2024-09-22 06:50:12 |
合計ジャッジ時間 | 4,436 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 9 |
ソースコード
package test_4;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;/*** No.423 ハムスター語初級(数詞)* 先見の明のあるあなたは、手始めにハムスター語の数字を学んでいる。* ハムスター語の数字の特徴は以下である。* ・小文字アルファベットのみからなる文字列である。* ・0以上の整数を表す。* ・"hamu"が1, "ham"が0を表す2進数表記である。* ・アラビア数字でleading zerosが許されていないように、ハムスター語でもleading hamsは許されていない。*/public class Question_11_0605_2 {static final int MIN = 3;static final int MAX = 50;public static void main(String[] args) {InputStreamReader re = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(re);try {String inputStr = br.readLine();if (StrJudg(inputStr, MIN, MAX)) {// 0スタートはハム語ではない(許されない)if (inputStr.length() >= 4 && inputStr.substring(0, 4).equals("hamh")) {System.out.println("ハム語ではありません");} else {// ハム語2進数変換String hum2Str = HamConversion(inputStr, 0);//2進数を10進数にint hum10Int = Integer.parseInt(hum2Str, 2);//10進数を2倍にint humTwice = hum10Int * 2;//10進数から2進数にhum2Str = Integer.toBinaryString(humTwice);// 2進数ハム語変換String humStr = HamConversion(hum2Str, 1);System.out.println(humStr);}} 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 str 判定する文字列* @param max 最大値* @param min 最小値* @return 範囲内ならtrue,範囲外ならfalseを返す*/private static boolean StrJudg(String input, int min, int max) {Boolean result = false;if (min <= input.length() && input.length() <= max) {result = true;}return result;}/*** ハム語を変換する* @param input 変換対象文字列* @param Type 変換方法 1:2進数 2:ハム語* @return 変換結果*/private static String HamConversion(String input, int Type) {String after = ""; //変換後// 2進数に直すif (Type == 0) {int lengthCount = 0;while (lengthCount < input.length()) {//残り4文字なければhamであるif (lengthCount + 4 > input.length()) {after += "0";lengthCount += 3;} else {if (input.substring(lengthCount, lengthCount + 4).equals("hamu")) {after += "1";lengthCount += 4;} else {after += "0";lengthCount += 3;}}}}// ハム語に直すif (Type == 1) {for (int i = 0; i < input.length(); i++) {if (input.substring(i, i + 1).equals("0")){after += "ham";} else {after += "hamu";}}}return after;}}