요세푸스 문제1158

LJM·2022년 12월 31일
0

백준풀기

목록 보기
4/259

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

처음과 끝에 "<", ">" 안 넣어서 틀린걸 모르고 3시간을 날렸네. 문제마다 정답의 양식이 다르다 보니 이런 황당한 일을 겪는다. 허탈하다...

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) {

        FastReader fr = new FastReader();

        String[] Line = fr.nextLine().split(" ");

        int N = Integer.parseInt(Line[0]);
        int K = Integer.parseInt(Line[1]);

        ArrayList<Integer> arrList = new ArrayList<>();
        for(int i = 1; i <= N; ++i)
        {
            arrList.add(i);
        }

        ArrayList<Integer> ret = new ArrayList();
        int idx = K-1;
        while(arrList.isEmpty() == false)
        {
            if(idx >= arrList.size())
            {
                idx -= arrList.size();
            }
            else
            {
                ret.add(arrList.remove(idx));
                idx += K-1;
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append("<");
        for(int i = 0; i < ret.size(); ++i)
        {
            if(i == ret.size()-1)
                sb.append(ret.get(i));
            else
                sb.append(ret.get(i) + ", ");
        }
        sb.append(">");

        System.out.println(sb.toString());
    }

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {

            while(st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            return str;
        }
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글