之前已經(jīng)寫在CSDN上了,現(xiàn)在也轉(zhuǎn)到這邊一下。
問題描述:
設(shè)有N個人圍坐一圈并按順時針方向從1到N編號,從第S個人開始進行1到M報數(shù),報數(shù)到第M個人時,此人出圈,再從他的下一個人重新開始1到M的報數(shù),如此進行下去直到所有的人都出圈為止。現(xiàn)要打印出出圈次序。
輸入:n 游戲總?cè)藬?shù) s 報數(shù)的起始編號 m 報數(shù)的數(shù)值
輸出:p 指向長度為n的數(shù)組,出圈次序保存在p指向的數(shù)組中
示例
n=7 s=2 m=3 出圈次序為:4 7 3 1 6 2 5
n=3 s=1 m=2 出圈次序為:2 1 3
1
#include <iostream>
2
#include <vector>
3
using namespace std;
4
/**//*
5
n=7 s=2 m=3 出圈次序為:4 7 3 1 6 2 5
6
n=3 s=1 m=2 出圈次序為:2 1 3
7
*/
8
void process(int n,int s,int m)
9

{
10
if(n<=0 || (s<1 || s>n) || m<=0)
11
{
12
cout<<"E8"<<endl;
13
return;
14
}
15
int MAX=n;
16
int start=s;
17
int step=m;
18
vector<int> men;
19
vector<int> outputList;
20
men.clear();
21
outputList.clear();
22
for (int i=0;i<MAX;i++)
23
men.push_back(i+1);
24
25
start=start-1;
26
27
int k=0;
28
while(!men.empty())
29
{
30
k=(start+step-1)%MAX;
31
outputList.push_back(men[k]);
32
men.erase(men.begin()+k);
33
MAX=men.size();
34
if (!MAX)
35
break;
36
start=k%MAX;
37
}
38
for (vector<int>::iterator it=outputList.begin();it!=outputList.end();it++)
39
cout<<*it<<" ";
40
cout<<endl;
41
}
42
int main()
43

{
44
int n,s,m;
45
while(cin>>n>>s>>m)
46
{
47
process(n,s,m);
48
}
49
return 0;
50
}

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
