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

[백준 2034] 반음 - JAVA

SH3542 2025. 3. 10. 20:23

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

 

프루트포스 문제

 

 

A = 0부터 피아노를 표현했을 때 G = 10이다.

 

조건에서, 다음 n개의 줄에는 절댓값이 20을 넘지 않는 정수로 악보가 주어진다.

10 + 20 = 30이므로,

한 옥타브를 표현한 배열 piano의 크기 12를 두 번 넘을 수 있음을 신경써야 한다. (음의 방향 또한 마찬가지)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

class Main {

  public static void main(String[] args) throws IOException {

    // true = 흰 건반, false = 검은 건반
    boolean[] piano = {true, false, true, true, false, true, false, true, true, false, true, false};

    int[] alpa = {0, 2, 3, 5, 7, 8, 10};
    Map<Integer, Character> dic = new HashMap<>();
    dic.put(0, 'A');
    dic.put(2, 'B');
    dic.put(3, 'C');
    dic.put(5, 'D');
    dic.put(7, 'E');
    dic.put(8, 'F');
    dic.put(10, 'G');

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int M = Integer.parseInt(br.readLine());
    int[] m = new int[M];
    for (int i = 0; i < M; i++) {
      m[i] = Integer.parseInt(br.readLine());
    }

    for (int i = 0; i < alpa.length; i++) {
      int cur = alpa[i];
      int st = cur;
      boolean isAns = true;

      for (int j = 0; j < M; j++) {

        cur += m[j];

        while (cur < 0) {
          cur += 12;
        }
        if (cur > 11) {
          cur %= 12;
        }

        if (!piano[cur]) {
          isAns = false;
          break;
        }
      }

      if (isAns) {
        System.out.println(dic.get(st) + " " + dic.get(cur));
      }
    }
  }
}