-
Notifications
You must be signed in to change notification settings - Fork 0
/
约瑟夫环正逆问题.cpp
118 lines (101 loc) · 2.29 KB
/
约瑟夫环正逆问题.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
题目描述:
约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(编号1,2,3…n)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。用双链表实现约瑟夫环问题,并输出顺时针与逆时针结果。
输入:
n m k
输出:
输出有两行,依次是约瑟夫环的顺时针和逆时针输出
样例输入:
5 2 1
10 3 7
样例输出:
2 4 1 5 3
5 3 1 2 4
9 2 5 8 3 7 4 1 6 10
5 2 9 6 1 7 10 3 8 4
*/
#include <iostream>
#include <string>
using namespace std;
struct Person
{
int num;
Person *before;
Person *next;
};
Person* Build(int n);
void Right(Person *head, int m, int k);
void Left(Person *head, int m, int k);
int main()
{
int n(0), m(0), k(0);
while(cin>>n)
{
cin >> m >> k;//input
Person *head1 = Build(n);
Person *head2 = Build(n);
Right(head1, m, k);
Left(head2, m, k);
}
return 0;
}
Person* Build(int n)
{
Person *head = new Person{ 1, nullptr, nullptr };
Person *p = head;
for (int i = 2; i <= n; ++i)
{
Person *add = new Person{ i,p,nullptr };
p->next = add;
p = p->next;
}
p->next = head;
head->before = p;
return head;
}
void Right(Person *head, int m, int k)
{
for (int i = 1; i < k; ++i)
head = head->next;
while (head->next != head)
{
//cout << "in" << endl;
for (int i = 1; i < m; ++i)
{
head = head->next;
}
//delete
cout << head->num << " ";
head->before->next = head->next;
head->next->before = head -> before;
Person *kill = head;
head = head->next;
delete kill;
}
cout << head->num << endl;
return;
}
void Left(Person *head, int m, int k)
{
for (int i = 1; i < k; ++i)
head = head->next;
while (head->before != head)
{
//cout << "in" << endl;
for (int i = 1; i < m; ++i)
{
//cout << "for" << endl;
//cout << head->before << endl;
head = head->before;
}
//delete
cout << head->num << " ";
head->before->next = head->next;
head->next->before = head->before;
Person *kill = head;
head = head->before;
delete kill;
}
cout << head->num << endl;
return;
}