• 문제 링크
31718번: Double Up
작업을 원하는 만큼 수행한 뒤, 배열에서 가장 많이 등장하는 수의 가능한 최대 등장 횟수를 출력한다.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Map<Integer, Integer> map = new HashMap<>();
int n = read(), max = 0;
while (n-- > 0) {
int a = getOddFactor(read());
map.put(a, map.getOrDefault(a, 0) + 1);
max = Math.max(max, map.get(a));
}
bw.write(String.valueOf(max));
bw.flush();
}
private static int getOddFactor(int a) {
while ((a & 1) == 0) a >>= 1;
return a;
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 31409 착신 전환 소동 - Graph Theory / Java (0) | 2024.04.04 |
---|---|
[백준] 31562 전주 듣고 노래 맞히기 - Data Structure / Java (0) | 2024.04.03 |
[백준] 25631 마트료시카 합치기 - Data Structure / Java (0) | 2024.04.01 |
[백준] 2776 암기왕 - Data Structure / Java (0) | 2024.03.31 |
[백준] 10815 숫자 카드 - Data Structure / Java (0) | 2024.03.30 |
댓글