250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준2251
- 알고리즘개념
- 자바
- 23288
- 자바 코테
- BFS
- 주사위굴리기2
- 정올 1620
- 파티션 크기 조정
- 코테준비
- Bfs와DFS
- 완탐
- N과M
- 백준13458
- 완전탐색
- 중복조합
- java
- 코테
- 순열
- 알고리즘
- 에라토스테네스의채
- 중복순열
- 삼성역테
- D드라이브생성
- 재귀함수
- 볼륨 만들기
- 전화번호속의암호
- 백준
- 정보처리기사
- 백준15652
Archives
- Today
- Total
뚱땅뚱땅
[문제] 백준 1158번 요세푸스 문제 본문
728x90
* www.acmicpc.net/problem/1158
1158번: 요세푸스 문제
첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 5,000)
www.acmicpc.net
내 풀이
큐에다 넣다뺐다 하면 된다. 큐로 풀 수 있다고 생각하면 쉬운 문제다.
public class BOJ_1158 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String s[] = in.readLine().split(" ");
sb.append("<");
int N = Integer.parseInt(s[0]);
int K = Integer.parseInt(s[1]);
Queue<Integer> queue = new LinkedList<Integer>();
for (int i = 1; i <= N; i++) {
queue.offer(i);
}
// 큐가 빌 때까지 반복
while (!queue.isEmpty()) {
for(int i=1;i<K;i++)
queue.offer(queue.poll()); //K-1개만큼 큐에서 빼기
sb.append(queue.poll()).append(", "); // K번째 큐 출력
}
sb.setLength(sb.length() - 2);
sb.append(">");
System.out.println(sb);
}
}
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[문제] 백준 7576번 토마토 (0) | 2021.02.11 |
---|---|
[문제] 백준 1966번 프린터 큐 (0) | 2021.02.11 |
[문제] 백준 1018번 체스판 다시 칠하기 (0) | 2021.02.09 |
[문제] 백준 2667번 단지번호 붙이기 (0) | 2021.02.08 |
[문제] 백준 1935번 후위 표기식2 (0) | 2021.02.07 |
Comments