© 2025 anveloper.dev
GitHub·LinkedIn·Contact

목차

  • 문제
  • 풀이
  • 코드
  • 복잡도
풀이 목록으로 돌아가기

BOJ 4732 - 조옮김

2026-03-13
BOJ
브론즈 III
cpp
원본 문제 보기
구현
문자열

문제

BOJ 4732 - 조옮김

음표들이 주어지면 지정된 반음 수만큼 이동시킨 결과를 출력하는 문제

풀이

12개 음계를 인덱스로 매핑하고, 샵/플랫 포함 음표를 파싱하여 해당 인덱스에 이동량을 더한 뒤 모듈로 12로 변환한다.

코드

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
 
using namespace std;
 
map<string, int> noteToIdx = {
    {"A", 0}, {"A#", 1}, {"Bb", 1}, {"B", 2}, {"Cb", 2}, {"B#", 3}, {"C", 3}, {"C#", 4}, {"Db", 4},
    {"D", 5}, {"D#", 6}, {"Eb", 6}, {"E", 7}, {"Fb", 7}, {"E#", 8}, {"F", 8}, {"F#", 9}, {"Gb", 9},
    {"G", 10}, {"G#", 11}, {"Ab", 11}
};
 
string idxToNote[] = {"A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"};
 
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
 
  string line;
  while (getline(cin, line) && line != "***") {
    vector<string> notes;
    stringstream ss(line);
    string temp;
    while (ss >> temp) notes.push_back(temp);
 
    int move;
    cin >> move;
    cin.ignore();
 
    for (int i = 0; i < notes.size(); i++) {
      int currentIdx = noteToIdx[notes[i]];
      int nextIdx = (currentIdx + move) % 12;
      if (nextIdx < 0) nextIdx += 12;
 
      cout << idxToNote[nextIdx] << (i == notes.size() - 1 ? "" : " ");
    }
    cout << "\n";
  }
  return 0;
}

복잡도

  • 시간: O(n) (n: 음표 개수)
  • 공간: O(n)

최근 글

  • 2026-03-15BOJ 4757 - A Contesting Decision
  • 2026-03-14BOJ 4749 - Take Your Vitamins
  • 2026-03-12BOJ 4697 - Fifty Coats of Gray
  • 2026-03-11BOJ 4678 - Skew Binary
  • 2026-03-10BOJ 4646 - Magnificent Meatballs
이전 글

BOJ 4697 - Fifty Coats of Gray

다음 글

BOJ 4749 - Take Your Vitamins