반응형
문제
Day 8: Dictionaries and Maps | HackerRank
Mapping Keys to Values using a Map or Dictionary.
www.hackerrank.com
문제 설명
맵을 이용해 푸는 문제
1 2 | //맵 선언 방법 Map<String, Integer> phoneBook = new HashMap<String, Integer>(); | cs |
성공 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Map<String, Integer> phoneBook = new HashMap<String, Integer>();
for(int i = 0; i < n; i++){
String name = in.next();
int phone = in.nextInt();
// Map에 저장
phoneBook.put(name, phone);
}
while(in.hasNext()){
String s = in.next();
// 전화부에 s가 있으면 번호 출력, 아니면 not found
if (phoneBook.containsKey(s))
System.out.println(s + "=" + phoneBook.get(s));
else
System.out.println("Not found");
}
in.close();
}
}
|
cs |
반응형