[백준] PS/Java [실랜디]

[백준 1755] 숫자놀이 - JAVA

SH3542 2025. 2. 20. 12:37

https://www.acmicpc.net/problem/1755

 

N-M구간 숫자에 대해 영어로 맵핑한 값으로 정렬, 숫자로 출력하는 문제

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

class Main {

  static class Num {

    String v;
    String s;

    Num(String v, String s) {
      this.v = v;
      this.s = s;
    }
  }

  public static void main(String[] args) {

    Map<String, String> m = new HashMap<>();
    m.put("0", "z");
    m.put("1", "o");
    m.put("2", "tw");
    m.put("3", "th");
    m.put("4", "fo");
    m.put("5", "fi");
    m.put("6", "si");
    m.put("7", "se");
    m.put("8", "e");
    m.put("9", "ni");

    Scanner sc = new Scanner(System.in);
    int M = sc.nextByte();
    int N = sc.nextByte();

    List<Num> l = new ArrayList<>();
    for (int i = M; i <= N; i++) {

      String s = String.valueOf(i);
      l.add(new Num(s, Arrays.stream(s.split(""))
              .map(m::get)
              .reduce((s1, s2) -> s1 + s2)
              .get()
          )
      );
    }

    Collections.sort(l, Comparator.comparing(num -> num.s));

    int space = 0;

    for (int i = 0; i < l.size(); i++) {

      System.out.print(l.get(i).v + " ");
      space++;

      if (space == 10) {
        System.out.println();
        space = 0;
      }
    }
  }
}