Skip to content

Commit af5c9a0

Browse files
authored
Create 约瑟夫环.cpp
1 parent c72176d commit af5c9a0

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

第一章/约瑟夫环.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int num,
7+
i,
8+
killed = 0,
9+
s = 0,
10+
t = 0;
11+
cin>>num;
12+
if (num==2)
13+
{
14+
cout<<"1 2";
15+
return 0;
16+
}
17+
int sur[num + 1];
18+
for (int i = 0; i <= num; i++)
19+
{
20+
sur[i] = 1;
21+
}
22+
do
23+
{
24+
++t;
25+
if (t > num)
26+
{
27+
t = 1;
28+
}
29+
if (sur[t] == 1)
30+
{
31+
s++;
32+
}
33+
if (s == 3)
34+
{
35+
s = 0;
36+
sur[t] = 0;
37+
killed++;
38+
}
39+
} while (num - killed > 2);
40+
for (int j = 1; j < num; j++)
41+
{
42+
if (sur[j] == 1)
43+
cout<<j<<" ";
44+
}
45+
}

0 commit comments

Comments
 (0)