728x90

<오늘의 학습 키워드>
- map
- 노가다..
<오늘의 문제>
제목 : 백준 29701 모스 부호

문제가 너무 길어서 링크로...
https://www.acmicpc.net/problem/29701
<풀이>
정답은 더보기
더보기
import java.io.*;
import java.util.*;
public class Main {
public static HashMap<String,String> makeMap() {
HashMap<String, String> map = new HashMap<>();
map.put(".-", "A");
map.put("-...", "B");
map.put("-.-.", "C");
map.put("-..", "D");
map.put(".", "E");
map.put("..-.", "F");
map.put("--.", "G");
map.put("....", "H");
map.put("..", "I");
map.put(".---", "J");
map.put("-.-", "K");
map.put(".-..", "L");
map.put("--", "M");
map.put("-.", "N");
map.put("---", "O");
map.put(".--.", "P");
map.put("--.-", "Q");
map.put(".-.", "R");
map.put("...", "S");
map.put("-", "T");
map.put("..-", "U");
map.put("...-", "V");
map.put(".--", "W");
map.put("-..-", "X");
map.put("-.--", "Y");
map.put("--..", "Z");
map.put(".----", "1");
map.put("..---", "2");
map.put("...--", "3");
map.put("....-", "4");
map.put(".....", "5");
map.put("-....", "6");
map.put("--...", "7");
map.put("---..", "8");
map.put("----.", "9");
map.put("-----", "0");
map.put("--..--", ",");
map.put(".-.-.-", ".");
map.put("..--..", "?");
map.put("---...", ":");
map.put("-....-", "-");
map.put(".--.-.", "@");
return map;
}
public static void main(String[] args) throws Exception {
Map<String,String> map = makeMap();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int count = Integer.parseInt(br.readLine());
String[] mosArray = br.readLine().split(" ");
for (int i = 0 ; i < mosArray.length; i++) {
if (map.containsKey(mosArray[i])) {
sb.append(map.get(mosArray[i]));
}
}
System.out.println(sb);
}
}
- map을 이용하기로 했다.
- 일단 입력이 "..- . _ ..." 이런식으로 들어오기 때문에 해당 부분을 key로 "A"는 value로 넣어주었다.
- 입력된 모스부호가 공백으로 구분해서 배열에 담기
- 배열을 돌면서 map의 key 중에서 일치하는 것의 value를 StringBuilder에 추가했다.
- 마지막에 출력
<결론, 주의할점>
- 백준의 코딩테스트 처음 풀어보는 사람은 정답을 제출할 때 당황할 수 있다. 그냥 아래 규칙만 지켜주면 됨
1. class이름은 반드시 Main
2. public static void main 함수 안에서 [입력 예제]를 주의해서 입력받고 출력 해주면 된다.
반응형
'Study or Lecture > 항해 99클럽 코딩테스트 스터디' 카테고리의 다른 글
| 항해 99클럽 코딩테스트 스터디 7일차 TIL (비기너) (3) | 2024.11.03 |
|---|---|
| 항해 99클럽 코딩테스트 스터디 6일차 TIL (비기너) (0) | 2024.11.02 |
| 99클럽 코딩테스트 스터디 4일차 TIL (비기너) (0) | 2024.10.31 |
| 99클럽 코딩테스트 스터디 3일차 TIL (비기너) (0) | 2024.10.30 |
| 99클럽 코딩테스트 스터디 2일차 TIL (비기너) (0) | 2024.10.29 |
댓글