How to solve the Joseph Ring problem with python
This article will explain in detail how to solve the Joseph ring problem in python. The content of the article is of high quality. Therefore, Xiaobian shares it with you as a reference. I hope that after reading this article, you will have a certain understanding of relevant knowledge.
Josephus ring problem: known n individuals (numbered 1, 2, 3... n respectively) sit around a round table. The number k is counted, and the person who counts k is killed; the next person counts from 1, and the person who counts k is killed; and so on until there is only one person left around the round table.
The idea is: when k is 1, the last person alive, when k>=2, construct a circular list of n elements, and then kill the kth person in turn, leaving the last person alive. The code is as follows:
class Node(): def __init__(self,value,next=None): self.value=value self.next=nextdef createLink(n): if n