[C++] 백준 11866번 요세푸스 문제 0

xyzw·2025년 3월 14일
0

algorithm

목록 보기
58/61

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

풀이

큐에서 k-1개의 수를 pop하고 push한 후, k번째 수를 pop한다.
이 과정을 n번 반복한다.

코드

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    
    int n, k;
    cin >> n >> k;
    
    vector<int> ans;
    
    queue<int> q;
    for(int i=1; i<=n; i++) q.push(i);
    
    while(!q.empty()) {
        int t;
        for(int i=0; i<k-1; i++) {
            t = q.front();
            q.pop();
            q.push(t);
        }
        t = q.front();
        q.pop();
        
        ans.push_back(t);
    }
    
    cout << "<";
    for(int i=0; i<n-1; i++) cout << ans[i] << ", ";
    cout << ans[n-1] << ">";
    
    return 0;
}

0개의 댓글