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
119
120
121
122
123
124
125 |
#include<iostream> #include<string.h> #include<cstdio> #include <sstream> using
namespace std; template
< class
T> class
joseph { struct
node { T data; node * next; node():next(NULL) {} node(T d):data(d),next(NULL) {} }; private : node * head; node * cur; node * pre; public : joseph(T d); int
len = 0; void
setValue(T d) { node * tem = new
node(d); cur ->next = tem; tem -> next = head; cur = cur -> next; pre = pre -> next; len++; } void
out() { pre -> next = cur -> next; cout<<cur -> data<< " " ; delete
cur; cur = pre -> next; len--; } void
nextmove() { cur = cur -> next; pre = pre -> next; } void
init() { cur = cur -> next; pre = pre -> next; } }; //模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性) template
< class
Type> Type stringToNum( const
string& str) { istringstream iss(str); Type num; iss >> num; return
num; } template
< class
T> joseph<T> :: joseph(T d) { head = new
node(d); node * fz = new
node(); cur = head; pre = fz; pre -> next = head; } int
main() { while (1) //总的循环 { string z,x; cout<< "请输入n个人" <<endl; cout<< "请输入密码m" <<endl; while (cin>>z,cin>>x) { int
a = 1 ; //为第一个节点赋值而创建的 int
j = 0 ; //j为n的输入判断因子,当j=1时说明输入的n不是int型 int
k = 0 ; //k为m的输入判断因子,当j=1时说明输入的n不是int型 joseph< int > dusk(a); //创建第一个节点,并调用构造函数把第一个节点赋值为1 dusk.len++; //链表长度加一 int
n , m ; //n为人数,m为密码 for ( int
i = 0 ; i < z.length() ; i++) //判断n是不是int型 { if (z[i]< ‘0‘ ||z[i]> ‘9‘ ) { cout<< "n input error" <<endl; j = 1; break ; } } n = stringToNum< int >(z); //调用函数把string型的n转换成int型 for ( int
i = 0 ; i < x.length() ; i++) //判断m是不是int型 { if (x[i]< ‘0‘ ||x[i]> ‘9‘ ) { cout<< "m input error" <<endl; k=1; break ; } } m = stringToNum< int >(x); //调用函数把string型的m转换成int型 if (k==1||j==1) break ; //判断因子有一个等于1说明:n,m有一个输入的不是int型,结束循环 for ( int
i = 2 ; i <= n ; i++) //初始化赋值 { dusk.setValue(i); } dusk.init(); //把cur指针指向head,把pre的next指向cur while (dusk.len!=0) //长度不为0时循环 { for ( int
i = 1 ; i < m ; i++) //移动 { dusk.nextmove(); } dusk.out(); } cout<<endl; } } } |
原文地址:http://www.cnblogs.com/Duskcl/p/3746547.html