-
백준 2164번 : 카드2알고리즘, SQL/알고리즘 2023. 12. 6. 23:59
https://www.acmicpc.net/problem/2164
별 생각 없이 배열 사용하고 짝수 인덱스번호인 애들만 삭제해주면 되겠다 싶어서 만들었었는데, 생각한대로 안되길래 한시간 넘게 문제를 잡고있었다. 뭔가 될 거 같아서 다른 블로그에서 힌트만 살짝 보려고하니까 큐를 사용해서 너무 쉽게 푸는거 보고 머리가 띵했다.
풀이
public class Main { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); Queue<Integer> q = new LinkedList<>(); for(int i=0; i<n; i++) { q.offer(i+1); } while(q.size()>1) { q.poll(); q.offer(q.poll()); } System.out.println(q.poll()); } }
반복수를 받아주고
반복수 만큼 큐를 채워준뒤,
큐에 데이터가 하나남을 때 까지 poll()사용해서 맨 앞의 데이터를 지워주면서 맨 뒤에다가 추가..
queue에 대해 정리를 한 번 해야겠다.
'알고리즘, SQL > 알고리즘' 카테고리의 다른 글
백준 7568번 : 덩치 (1) 2023.12.12 queue란 무엇인가? (0) 2023.12.10 HashSet이란? (0) 2023.12.02 백준 3052번 : 나머지 (0) 2023.11.28 백준 1157번 : 단어 공부 (2) 2023.11.27