[BOJ] 1158번 : 요세푸스
2024. 8. 6. 09:36ㆍAlgorithm
1. problem :
https://www.acmicpc.net/problem/1158
2. solution 1 :
from collections import deque
N,K = map(int,input().split())
deq = deque([i+1 for i in range(N)])
count = 0
result = []
while deq:
count += 1
if count == K:
result.append(deq.popleft())
count = 0
else:
deq.append(deq.popleft())
print('<' +', '.join(map(str,result)) + '>')
deque을 이용한 풀이
3. solution 2 :
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int N, K;
cin >> N >> K;
vector<int> v;
vector<int> ans;
for (int i = 0; i < N; i++) {
v.push_back(i + 1);
}
int i = 0;
while (v.size() > 0) {
i = (i + K - 1) % v.size();
ans.push_back(v[i]);
v.erase(v.begin() + i);
}
// 포맷팅하여 출력
cout << '<';
for (size_t j = 0; j < ans.size(); j++) {
cout << ans[j];
if (j < ans.size() - 1) {
cout << ", ";
}
}
cout << '>' << '\n';
return 0;
}
배열을 이용한 풀이
'Algorithm' 카테고리의 다른 글
[BOJ]1874번 : 스택 수열 (0) | 2024.08.06 |
---|---|
[BOJ]10773번 : 제로 (0) | 2024.08.06 |
[BOJ] 5397번 : 키로거 (0) | 2024.08.05 |
[BOJ] 1406: 에디터 (0) | 2024.08.05 |
[Boj]1919: 애너그램 만들기 (0) | 2024.08.04 |