fork download
  1. def josephus(n, k):
  2. if n == 1:
  3. return 1
  4. else:
  5. return (josephus(n - 1, k) + k - 1) % n + 1
  6.  
  7. n = 301
  8. k = 2
  9. survivor = josephus(n, k)
  10. print("最後存活者的編號是:", survivor)
  11.  
Success #stdin #stdout 0.02s 7416KB
stdin
Standard input is empty
stdout
('\xe6\x9c\x80\xe5\xbe\x8c\xe5\xad\x98\xe6\xb4\xbb\xe8\x80\x85\xe7\x9a\x84\xe7\xb7\xa8\xe8\x99\x9f\xe6\x98\xaf:', 91)