Search
Duplicate
🍋

요세푸스 문제

주차
17
문제번호
1158
언어
C++
티어
실버
유형
자료구조
nj_Blog
nj_상태
이해도
풀이
풀이 O
사람
13 more properties

Memo

큐를 사용해서 계속 빼고 넣고 하면 된다!!
K - 1만큼 빼고 1개는 pop!!

Code

제출 날짜

@4/23/2021

메모리

2148 KB

시간

64 ms
#include <iostream> #include <queue> int N, K; std::queue<int> yos; std::vector<int> res; void output() { std::cout << "<"; for(auto& i : res) { std::cout << i; if (i == res[N - 1]) break; std::cout << ", "; } std::cout << ">"; } void solution() { int tmp; while(!yos.empty()) { for(int i = 0 ; i < K - 1 ; ++i) { tmp = yos.front(); yos.pop(); yos.push(tmp); } res.push_back(yos.front()); yos.pop(); } } void input() { std::cin >> N >> K; for (int i = 1 ; i <= N ; ++i) yos.push(i); } void preset() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); } int main() { preset(); input(); solution(); output(); }
C++
복사