본문 바로가기
[ 다먹살 ]/- Coding

[백준] 1158 요세푸스 문제

by 엉망으로살기 2022. 4. 11.
반응형

https://www.acmicpc.net/problem/1158

 


문제 및 입출력

 


코드

import java.util.Scanner;
import java.util.ArrayList;

public class Main
{
    public static void main(String[] args)
    {
        // 기본 사용변수 초기화설정 및 입력값 처리
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        ArrayList<Integer> list = new ArrayList<Integer>();
        
        sb.append("<");
        int n = sc.nextInt();
        int k = sc.nextInt();
        int cnt = 1;
        int index = 0;
        
        // 1~n까지 숫자 삽입
        for(int i=1; i<=n; i++)
        {
            list.add(i);
        }
        
        // 리스트가 빌 때까지 반복
        while(!list.isEmpty())
        {
            index = index%list.size();
            
            if(cnt==k)
            {
                sb.append(list.get(index) + ", ");
                list.remove(index);
                cnt = 1;
            }
            else
            {
                cnt++;
                index++;
            }
        }
        
        // 결과값 처리 후 출력
        System.out.println(sb.toString().substring(0, sb.length()-2) + ">");
        sc.close();
    }
}

 

반응형

'[ 다먹살 ] > - Coding' 카테고리의 다른 글

[백준] 11655 ROT13  (0) 2022.04.13
[백준] 10808 알파벳 개수  (0) 2022.04.12
[백준] 20291 파일 정리  (0) 2022.04.08
[백준] 14495 피보나치 비스무리한 수열  (0) 2022.04.08
[백준] 9625 BABBA  (0) 2022.04.07

댓글