프로그래밍 공부
BOJ/백준 11866 요세푸스 문제 0 본문
https://www.acmicpc.net/problem/11866
해결 아이디어
대놓고 큐를 써보는 문제
카운터 변수를 두고 k횟수 만큼 조건이 채워지면 pop 하는식으로 큐가 empty 해질때 까지 반복
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
vector<int> ans;
queue<int> q;
cin >> n >> k;
for(int i = 1; i < n + 1; i++) q.push(i);
int counter = 0;
while(!q.empty()){
if(counter == k - 1){
ans.push_back(q.front());
q.pop();
counter = 0;
}
else{
int tmp = q.front();
q.pop();
q.push(tmp);
counter++;
}
}
int len = ans.size();
cout << "<";
for(int i = 0; i < len; i++){
if(i == len - 1) cout << ans[i] << ">";
else cout << ans[i] << ", ";
}
return 0;
}
'Problem Solving > Baekjoon Online Judge' 카테고리의 다른 글
BOJ/백준 3190 뱀 (0) | 2024.09.11 |
---|---|
BOJ/백준 13335 트럭 (0) | 2024.09.10 |
BOJ/백준 2304 창고 다각형 (1) | 2024.09.09 |
BOJ/백준 2504 괄호의 값 (0) | 2024.09.09 |
BOJ/백준 17952 과제는 끝나지 않아! (0) | 2024.08.18 |
Comments