일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- 프로그래머스 자바
- 프로그래머스
- 백준 2660번 회장뽑기 G5
- map
- 서브모듈 yml
- 백준 1600번 말이 되고픈 원숭이
- HashMap
- dp
- 코틀린기초
- 스프링 on-profile
- 전위 중위 후위
- 백준 8979번 올림픽 S5 자바
- HashSet
- hash
- Stack
- 포인트 컷
- StringBuilder
- 백준 11725번 트리의 부모 찾기
- replace()
- kotlin
- StringTokenizer
- 프로그래머스 java
- 백준 2206번 벽 부수고 이동하기 G3
- 백준 2589번 보물섬 G5
- Java
- toUpperCase
- 스프링 다중프로필
- append
- 백준 1240번 노드사이의 거리
- 백준 1967번 트리의 지름 G4 자바
Archives
- Today
- Total
말하는 컴공감자의 텃밭
백준 11866번 요세푸스 문제 0 <S5> - 구현 본문
728x90
K번째 숫자를 지워주면 된다. 쉽지용
queue를 활용해 줍니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import java.util.*; public class Main { //11866번 요세푸스 문제 0 56% // 큐 활용. public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int idx = 0; StringBuilder sb = new StringBuilder(); Queue<Integer> queue = new LinkedList<Integer>(); Queue<Integer> answer = new LinkedList<Integer>(); // Input for (int i = 1; i <= n; i++) { queue.add(i); } // logic while(answer.size() < n){ // 순열이 담길때까지 큐가 빌때까지나 동일 idx ++; if(idx == k) { // k번째마다 idx = 0; answer.add(queue.poll()); // 큐 맨앞을 answer에 넣기 }else { int p = queue.poll(); // 아니라면 맨앞을 뒤로 옮기기 (원) queue.add(p); } } //output sb.append("<"); // < x,x,x,x,x > 형태로 출력 위함 while(!answer.isEmpty()) { sb.append(answer.poll()); if(!answer.isEmpty()) { sb.append(", "); } } sb.append(">"); System.out.println(sb); sc.close(); }// main }// solution | cs |
728x90
'알고리즘 > Backjoon - Java' 카테고리의 다른 글
백준 11403번 경로 찾기 S1 - 플로이드 위샬, BFS (0) | 2023.11.15 |
---|---|
백준 골드 V 짜자작 (1) | 2023.11.14 |
백준 1260번 DFS와 BFS <S2> - DFS BFS (0) | 2023.11.05 |
백준 1912번 연속합 <S2> - 수학 (0) | 2023.11.04 |
백준 2108번 통계학 <S3> - 수학 (0) | 2023.11.03 |
Comments