Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1691788

Browse files
committed
圆圈中最后剩下的数字
1 parent fe45f2c commit 1691788

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

sixth/fourth/fourty_five.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# coding=utf-8
2+
"""
3+
0到n-1排成一圈,从0开始每次数m个数删除,求最后剩余的数
4+
当 n > 1 时: f(n,m) = [f(n-1, m)+m]%n,当 n = 1 时: f(n,m)=0
5+
"""
6+
7+
8+
def last_num(n, m):
9+
ret = 0
10+
if n == 1:
11+
return 0
12+
for i in range(2, n+1):
13+
ret = (m + ret) % i
14+
return ret
15+
16+
if __name__ == '__main__':
17+
print last_num(3, 4)

0 commit comments

Comments
 (0)