문제: https://www.acmicpc.net/problem/1158
유명한 josephus 문제입니다. linked list 을 이용해 풀었습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #pragma warning (disable:4996) #include <cstdio> #include <cstring> #include <climits> #include <algorithm> #include <list> using namespace std; void josephus(int n, int m) { list<int> survivors; for (int i = 1; i <= n; i++) { survivors.push_back(i); } list<int>::iterator dead = survivors.begin(); for (int i = 0; i < m - 1; i++) { dead++; } printf("<"); while (true) { printf("%d", *dead); dead = survivors.erase(dead); if (dead == survivors.end()) { dead = survivors.begin(); } n--; if (n == 0) { break; } printf(", "); for (int i = 0; i < (m - 1) % n; i++) { dead++; if (dead == survivors.end()) { dead = survivors.begin(); } } } printf(">"); } int main() { int n, m; scanf("%d %d", &n, &m); josephus(n, m); return 0; } | cs |
'Algorithm, Data structure > Solved Algorithmic Problem' 카테고리의 다른 글
BAEKJOON 1932 - 숫자삼격형 (0) | 2018.02.01 |
---|---|
BAEKJOON 1149 - RGB거리 (0) | 2017.12.22 |
BAEKJOON 2343 - 기타 레슨 (0) | 2017.09.16 |
BAEKJOON 1992 - 쿼드트리 (0) | 2017.01.05 |
BAEKJOON 2792 - LJUBOMORA (0) | 2017.01.05 |