-
Notifications
You must be signed in to change notification settings - Fork 0
/
m-Josephus.cpp
51 lines (46 loc) · 1.07 KB
/
m-Josephus.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
/*************************************************************************
> File Name: m-Josephus.cpp
> Author: Louis1992
> Mail: zhenchaogan@gmail.com
> Blog: http://gzc.github.io
> Created Time: Wed Jul 15 11:49:42 2015
************************************************************************/
#include<iostream>
using namespace std;
struct node
{
int no;
node *next;
node(int i){no = i;}
};
int main()
{
int m,n;
node *cur,*q,*first;
cout<<"请输入m的初始值 m:"; cin>>m;
cout<<"请输入人数 n:"; cin>>n;
for(int i = 1;i<=n;i++)
{
if(i == 1)
first = cur = new node(i);
else
{
q = new node(i);
cur->next = q;
cur = q;
}
}
cur->next = first;
cur = first;
cout<<"出列顺序为: ";
for (int j = 1;j<=n;j++)
{
for(int i = 1; i < m;i++,cur=cur->next);
cout << cur->no <<" ";
cur->no = cur->next->no;
q = cur->next;
cur->next = cur->next->next;
}
cout<<endl;
return 0;
}