diff --git a/rooki/src/C030.java b/rooki/src/C030.java new file mode 100644 index 0000000000000000000000000000000000000000..e0b0f3f5945806aeb134f1c2577d2846baa4e6d6 --- /dev/null +++ b/rooki/src/C030.java @@ -0,0 +1,119 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/** + * グレースケール画像を二値画像に変換. + * + * @author rooki + * @version 4.0 + */ + +public class C030 { + + /** + * mainメソッド. + * + * @param args 使用しない + */ + public static void main(final String[] args) { + + int numberCount = 1; + + final List> pixelValueList = getInputData(); + + for (int line = 0; line < pixelValueList.size(); line++) { + for (int column = 0; column < pixelValueList.get(line).size(); column++) { // 行*列の回数繰り返す + // 画素値を二値化し、画像の色を代入する + final Color binaryColor = getColor(pixelValueList.get(line).get(column)); + + outputResult(binaryColor, numberCount, column); // 列の数を整え、画像に対応した色を出力 + + numberCount += 1; // 何個目の数値かカウントアップしながら数える + + } + } + } + + /** + * 入力処理. + * + * + */ + public static List> getInputData() { + final Scanner sc = new Scanner(System.in); + + final int lineCount = sc.nextInt(); // 行の数を取得 + final int columnCount = sc.nextInt(); // 列の数を取得 + + final List> pixelValueListOfLine = new ArrayList<>(); + + for (int line = 0; line < lineCount; line++) { + final List pixelValueListOfColumn = new ArrayList<>(); + for (int column = 0; column < columnCount; column++) { + final int pixelValue = sc.nextInt(); + pixelValueListOfColumn.add(pixelValue); + } + pixelValueListOfLine.add(pixelValueListOfColumn); + } + sc.close(); + + return pixelValueListOfLine; + } + + /** + * 受け取った値を二値化して返す. + * + * @param pixelValue 画素値を受け取る + * @return 二値化した画像の色を返す + */ + public static Color getColor(final int pixelValue) { + if (pixelValue <= 127) { + return Color.BLACK; + } + return Color.WHITE; + } + + /** + * 取得した列の数に合わせて出力. + * + * @param color 与えられた画像の色 + * @param numberCount その数値が全体の何個目であるか + * @param columnCount 何列にして出力するか + */ + public static void outputResult(final Color color, + final int numberCount, final int columnCount) { + if (numberCount % columnCount == 0) { + System.out.println(color.getValue()); + } else { + System.out + .print(color.getValue() + + " "); + } + } + + /** + * 色の種別を持つ列挙型クラス. + * + */ + public enum Color { + + BLACK(0), + + WHITE(1); + + // フィールド + private final int value; + + // コンストラクタ + private Color(int value) { + this.value = value; + } + + // メソッド + public int getValue() { + return value; + } + + } +}