1
+ {
2
+ "nbformat" : 4 ,
3
+ "nbformat_minor" : 2 ,
4
+ "metadata" : {
5
+ "language_info" : {
6
+ "name" : " python" ,
7
+ "codemirror_mode" : {
8
+ "name" : " ipython" ,
9
+ "version" : 3
10
+ },
11
+ "version" : " 3.7.6-final"
12
+ },
13
+ "orig_nbformat" : 2 ,
14
+ "file_extension" : " .py" ,
15
+ "mimetype" : " text/x-python" ,
16
+ "name" : " python" ,
17
+ "npconvert_exporter" : " python" ,
18
+ "pygments_lexer" : " ipython3" ,
19
+ "version" : 3 ,
20
+ "kernelspec" : {
21
+ "name" : " python3" ,
22
+ "display_name" : " Python 3"
23
+ }
24
+ },
25
+ "cells" : [
26
+ {
27
+ "cell_type" : " code" ,
28
+ "execution_count" : 85 ,
29
+ "metadata" : {},
30
+ "outputs" : [
31
+ {
32
+ "output_type" : " stream" ,
33
+ "name" : " stdout" ,
34
+ "text" : " The total count is: 4. The step number is: 2\n The final position is: 0\n "
35
+ }
36
+ ],
37
+ "source" : [
38
+ " # Filename: python_practice2.ipynb\n " ,
39
+ " #\n " ,
40
+ " # Summary: 约瑟夫环\n " ,
41
+ " #\n " ,
42
+ " # Date: 2020/4/7\n " ,
43
+ " #\n " ,
44
+ " # author by: 崇森([email protected] )\n " ,
45
+ " \n " ,
46
+ " \n " ,
47
+ " # @FullName: cycle_end\n " ,
48
+ " #\n " ,
49
+ " # @Brief: 约瑟夫环从 0 开始循环,得出最后留下的序号\n " ,
50
+ " #\n " ,
51
+ " # @Parameter: 总数,步进\n " ,
52
+ " #\n " ,
53
+ " # @Return: 留下的序号\n " ,
54
+ " def cycle_end(length, step):\n " ,
55
+ " position = 0\n " ,
56
+ " \n " ,
57
+ " low_bound = 2\n " ,
58
+ " up_bound = length + 1\n " ,
59
+ " for i in range(low_bound, up_bound):\n " ,
60
+ " position = (position + step) % i\n " ,
61
+ " \n " ,
62
+ " return position\n " ,
63
+ " \n " ,
64
+ " \n " ,
65
+ " assert(cycle_end(1, 0) == 0)\n " ,
66
+ " assert(cycle_end(2, 2) == 0)\n " ,
67
+ " assert(cycle_end(2, 3) == 1)\n " ,
68
+ " assert(cycle_end(3, 2) == 2)\n " ,
69
+ " assert(cycle_end(3, 3) == 1)\n " ,
70
+ " \n " ,
71
+ " total_count = int(input(\" Please input the total count\" ))\n " ,
72
+ " step_num = int(input(\" Please input the step number\" ))\n " ,
73
+ " print(\" The total count is: %d. The step number is: %d\" % (total_count, step_num))\n " ,
74
+ " \n " ,
75
+ " final_position = cycle_end(total_count, step_num)\n " ,
76
+ " print(\" The final position is:\" , final_position)\n "
77
+ ]
78
+ },
79
+ {
80
+ "cell_type" : " code" ,
81
+ "execution_count" : 84 ,
82
+ "metadata" : {},
83
+ "outputs" : [
84
+ {
85
+ "output_type" : " stream" ,
86
+ "name" : " stdout" ,
87
+ "text" : " The total count is: 4. The step number is: 2\n The final position is: 1\n "
88
+ }
89
+ ],
90
+ "source" : [
91
+ " # @FullName: cycle_end\n " ,
92
+ " #\n " ,
93
+ " # @Brief: 约瑟夫环从 1 开始循环,得出最后留下的序号\n " ,
94
+ " #\n " ,
95
+ " # @Parameter: 总数,步进\n " ,
96
+ " #\n " ,
97
+ " # @Return: 留下的序号\n " ,
98
+ " def cycle_end(length, step):\n " ,
99
+ " position = 1\n " ,
100
+ " \n " ,
101
+ " low_bound = 2\n " ,
102
+ " up_bound = length + 1\n " ,
103
+ " for i in range(low_bound, up_bound):\n " ,
104
+ " position += step\n " ,
105
+ " if position % i == 0:\n " ,
106
+ " position = i\n " ,
107
+ " continue\n " ,
108
+ " \n " ,
109
+ " position %= i\n " ,
110
+ " \n " ,
111
+ " return position\n " ,
112
+ " \n " ,
113
+ " \n " ,
114
+ " assert(cycle_end(1, 0) == 1)\n " ,
115
+ " assert(cycle_end(2, 2) == 1) \n " ,
116
+ " assert(cycle_end(2, 3) == 2)\n " ,
117
+ " assert(cycle_end(3, 2) == 3)\n " ,
118
+ " assert(cycle_end(3, 3) == 2)\n " ,
119
+ " \n " ,
120
+ " total_count = int(input(\" Please input the total count\" ))\n " ,
121
+ " step_num = int(input(\" Please input the step number\" ))\n " ,
122
+ " print(\" The total count is: %d. The step number is: %d\" % (total_count, step_num))\n " ,
123
+ " \n " ,
124
+ " final_position = cycle_end(total_count, step_num)\n " ,
125
+ " print(\" The final position is:\" , final_position)\n " ,
126
+ " "
127
+ ]
128
+ },
129
+ {
130
+ "cell_type" : " code" ,
131
+ "execution_count" : 83 ,
132
+ "metadata" : {},
133
+ "outputs" : [
134
+ {
135
+ "output_type" : " stream" ,
136
+ "name" : " stdout" ,
137
+ "text" : " The last remaining element is: 2\n "
138
+ }
139
+ ],
140
+ "source" : [
141
+ " # @FullName: cycle_end\n " ,
142
+ " #\n " ,
143
+ " # @Brief: 列表实现约瑟夫环,得出最后留下的元素\n " ,
144
+ " #\n " ,
145
+ " # @Parameter: 列表,步进\n " ,
146
+ " #\n " ,
147
+ " # @Return: 留下的元素\n " ,
148
+ " def cycle_end(target, step):\n " ,
149
+ " position = 0\n " ,
150
+ " \n " ,
151
+ " while len(target) > 1:\n " ,
152
+ " position = (position + step - 1) % len(target)\n " ,
153
+ " del target[position]\n " ,
154
+ " \n " ,
155
+ " return target[0]\n " ,
156
+ " \n " ,
157
+ " \n " ,
158
+ " assert(cycle_end([0, 1], 2) == 0)\n " ,
159
+ " assert(cycle_end([0, 1, 2], 2) == 2)\n " ,
160
+ " assert(cycle_end([0, 1, 2], 3) == 1)\n " ,
161
+ " \n " ,
162
+ " sequence = [2, 4, 3, 5]\n " ,
163
+ " step_num = 2\n " ,
164
+ " print(\" The last remaining element is:\" , cycle_end(sequence, step_num))\n " ,
165
+ " "
166
+ ]
167
+ }
168
+ ]
169
+ }
0 commit comments