접근 유형 : 큐
접근 방식
N = 7, K = 3 인 경우
1 2 3 4 5 6 7
4 5 6 7 1 2
7 1 2 4 5
4 5 7 1
1 4 5
1 4
public class Main_1158 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();//사람 수
int K = sc.nextInt();
Queue<Integer> queue = new LinkedList<Integer>();
//1~N번까지의 사람들을 넣음
for(int i = 1; i <= N; i++) {
queue.add(i);
}
StringBuilder sb = new StringBuilder();
int[] result = new int[N];
int idx = 0;
while(!queue.isEmpty()) {
//k-1번째에 해당하는 데이터를 빼내야함
for(int i = 1; i <= K-1; i++) {
queue.add(queue.poll());
}
//K번째 해당하는 사람
int people_K = queue.poll();
result[idx++] = people_K;
}
System.out.print("<");
for(int i = 0; i < N; i++) {
if(i == N-1) {
System.out.print(result[i]);
}else {
System.out.print(result[i] + ", ");
}
}
System.out.println(">");
sc.close();
}
}
'Algorithm > 백준' 카테고리의 다른 글
[BOJ 백준] 11403 경로 찾기 - 자바 (0) | 2022.09.04 |
---|---|
[BOJ 백준] 2606번 바이러스 - 자바 (0) | 2022.08.30 |
[백준] 백설 공주와 일곱 난쟁이(자바) (0) | 2022.08.11 |
[백준] 6603번 로또 (자바) (0) | 2022.08.09 |
[백준] 2164번 카드2 문제(자바) (0) | 2022.08.04 |