[11866] 요세푸스 문제 0

RudinP·2023년 5월 3일
0

BaekJoon

목록 보기
64/77

생각

1234567 이 있다 치면
3 6 2 7 5 1 4
큐를 이용해서 k-1번씩 dequeue하자마자 enqueue 해주면 된다
그리고 Dequeue 한걸 sb에 추가한다.
이걸 n번 반복한다.
456712

처음 코드

using System.Text;

namespace SongE
{
    public class Program
    {
        static void Main(string[] args)
        {
            int[] nk = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            Console.WriteLine(Josephus(nk[0], nk[1]));
        }

        static string Josephus(int n, int k)
        {
            StringBuilder sb = new StringBuilder("<");
            Queue<int> q = new Queue<int>();

            for(int i = 1; i <= n; i++)
            {
                q.Enqueue(i);
            }

            for(int i = 0; i < n; i++)
            {
                for(int j = 1; j < k; j++)
                    q.Enqueue(q.Dequeue());

                sb.Append(q.Dequeue() + ", ");
            }
            sb.Remove(sb.Length - 2, 2);
            sb.Append(">");
            return sb.ToString();
        }
 
    }

}

profile
곰을 좋아합니다. <a href = "https://github.com/RudinP">github</a>

0개의 댓글