http://183.106.113.109/30stair/josephus/josephus.php?pname=josephus
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <iostream> #include <string> #include <stdlib.h> using namespace std; typedef struct _node { int num; struct _node* prev; struct _node* next; }node; node* CreateNode(int num); void RemoveNode(node* head, int startNum, int difNum); node arr[1000]; int n, m, k; int main() { node* Head; cin >> n >> m >> k; // Creat a List for (int i = 1; i <= n; i++) { if (i == 1) Head = CreateNode(1); else { node* tmpNode = Head; while (1) { if (tmpNode->next == NULL) { tmpNode->next = CreateNode(i); tmpNode->next->prev = tmpNode; break; } tmpNode = tmpNode->next; } } } node* tmpNode = Head; while (1) { if (tmpNode->next == NULL) { tmpNode->next = Head; Head->prev = tmpNode; break; } tmpNode = tmpNode->next; } RemoveNode(Head, m, k); return 0; } node* CreateNode(int num) { node* NewNode = (node*)malloc(sizeof(node)); NewNode->num = num; NewNode->next = NULL; return NewNode; } void RemoveNode(node* head, int startNum, int difNum) { int start = startNum, nextNum = 0, dif = difNum; node* tmpNode = head; int cnt = 0; while (1) { if (tmpNode->next->num == tmpNode->num && cnt == 0) cnt++; else if (cnt == 1) break; if (tmpNode->num == startNum || tmpNode->num == nextNum) { cout << tmpNode->num << " "; // 연결 tmpNode->prev->next = tmpNode->next; tmpNode->next->prev = tmpNode->prev; tmpNode = tmpNode->next; // 다음꺼 node* tmp = tmpNode; for (int i = 1; i < dif; i++) { tmp = tmp->next; } nextNum = tmp->num; continue; } tmpNode = tmpNode->next; } } |