알고리즘/🥈 실버
백준 11866 요세푸스 문제0 자바 풀이
차디러루너
2023. 7. 9. 21:28
728x90
난이도 : 실버5
풀이일 : 07097
https://www.acmicpc.net/problem/11866
11866번: 요세푸스 문제 0
첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 1,000)
www.acmicpc.net
링크로 이동하기 귀찮은 분들을 위한 문제 캡쳐
풀이 코드
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int K = scan.nextInt();
LinkedList<Integer> queue = new LinkedList<>();
// queue 요소 추가
for (int i = 1; i <= N; i++) {
queue.add(i);
}
System.out.print("<");
// K 번째 수 출력 N-1회 반복
for (int j = 0; j < N-1; j++) {
for (int k = 0; k < K-1; k++) {
queue.addLast(queue.pollFirst());
}
System.out.print(queue.poll()+", ");
}
// 마지막 요소 출력
System.out.println(queue.poll()+">");
}
}
느낀점
지난번에 푼 요세푸스 문제랑 아주 똑같은 문제였다.
오늘의 파이썬 문제 풀이가 제 시간안에 끝나지 않을까봐 자바 문제 부터 후딱 풀이 올려버리기