본문 바로가기
Algorithm/백준

[백준 BOJ] 1158 요세푸스 문제(자바)

by 코딩로그 2022. 8. 9.

접근 유형 : 큐

 

접근 방식

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();
	}

}