알고리즘 및 자료구조/문제
백준알고리즘 1159번 농구 경기
ktko
2018. 4. 24. 13:27
백준 알고리즘
https://www.acmicpc.net/problem/1159
딱히 고민할 것 없었던 문제.
Map에다가 첫 번째 문자를 저장하고 Iterator을 이용해서 Map의 Value가 5이상인 것만 찾아서 더해주면 해결된다.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int count = Integer.parseInt(scan.nextLine().trim()); Map<Character, Integer> nameMap = new HashMap<Character, Integer>(); for(int i = 0; i < count; i++) { String name = scan.nextLine().trim(); char firstChar = name.charAt(0); if(nameMap.get(firstChar) != null) { int a = nameMap.get(firstChar); a++; nameMap.put(firstChar, a); } else { nameMap.put(firstChar, 1); } } Iterator<Character> mapIter = nameMap.keySet().iterator(); String result = ""; while(mapIter.hasNext()) { Character key = mapIter.next(); int value = nameMap.get(key); if(value >= 5) { result += key; } } if("".equals(result)) { System.out.println("PREDAJA"); } else { System.out.println(result); } } }