본문 바로가기

백준

BOJ_1158 요세푸스 문제

백준 링크 : https://www.acmicpc.net/problem/1158

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        LinkedList<Integer> List = new LinkedList<>();
        int N = sc.nextInt(); // 사람 수
        int K = sc.nextInt(); // 제거될 순서

        //링크드리스트에 사람 수만큼 입력
        for(int i=1;i<=N;i++) List.add(i);

        //출력
        System.out.print("<");

        //링크드리스트가 빌 때까지 반복
        while(!List.isEmpty()) {
            //제거될 순서만큼 반복
            for(int i=0;i<K;i++) {
                //K번이 되면 리스트에서 삭제하고,
                if(i==K-1) {
                    int a = List.remove();
                    //링크드리스트 크기가 0이라면 마지막엔 쉼표 없이 출력
                    if(List.size()==0) {
                        System.out.print(a);
                    }
                    else System.out.print(a + ", ");
                }
                //K번이 아니라면 삭제 후 다시 넣기
                else {
                    List.add(List.remove());
                }
            }
        }
        System.out.print(">");
    }
}

 

'백준' 카테고리의 다른 글

BOJ_1515 수 이어쓰기  (0) 2023.01.24
BOJ_1446 지름길  (0) 2023.01.24
BOJ_4659 비밀번호 발음하기  (0) 2023.01.24
BOJ_1406 에디터  (0) 2023.01.16
BOJ_9655 돌 게임  (0) 2023.01.16