python solve the Josephus problem (easy to understand version)

python solve the Josephus problem (easy to understand version)

Josephus problem: Given n individuals (with numbers 1,2,3 ... n represent, respectively) were sitting around a round table. From the number of people began to count off k, k is the number of the person to be killed; he is the next person to person and from 1 Countin number to k has been killed; and so the law is repeated until who only last around a round table.

The first blog, please exhibitions.

Super easy to understand version:
idea : just started to put a list of all the people to go inside, reported figures are not final 3 to put the person into a position above the list to go, if it is put that number 3 is removed from the list . The remaining two people up until the list, as follows:

def josephus(n,k):
    #n代表总人数,k代表报数的数字
    List = list(range(1,n+1))
    index = 0
    while List:
        temp = List.pop(0)
        index += 1
        if index == k:
            index = 0
            continue
        List.append(temp)
        if len(List)==2:
            print(List)
            break

if __name__ == '__main__':
    josephus(41,3)

-------------------------------------------------- - I'm dividing line --------------------------------------------- ---------------------

One-way circular list method (method in order to consolidate the knowledge of the list to stop the use of)
thinking : is the use of one-way circulation list, in fact, with a method similar to the above, the following code:

class Node(object):
    """结点"""
    def __init__(self,item):
        self.elem = item
        self.next = None

class SingleCycleLinkList(object):
    """单向循环链表"""
    def __init__(self):
        self.head = None

    def append(self,item):
        node = Node(item)
        if self.head is None:
            self.head = node
            node.next = node
        else:
            cur = self.head
            while cur.next != self.head:
                cur = cur.next
            cur.next = node
            node.next = self.head

    def travel(self):
        cur = self.head
        while cur.next != self.head:
            print(cur.elem, end=" ")
            cur = cur.next
        print(cur.elem)

    def remove(self,item):
        '''删除节点'''
        cur = self.head
        pre = None
        while cur.next != self.head:
            if cur.elem == item:
                #头节点的情况
                if cur == self.head:
                    rear = self.head
                    while rear.next != self.head:
                        rear = rear.next
                    rear.next = cur.next
                    self.head = cur.next
                else:
                    #中间结点的情况
                    pre.next = cur.next
                return
            else:
                pre = cur
                cur = cur.next
    #尾节点的情况和一个元素的情况
        if cur.elem == item:
                # 一个元素的情况
            if cur == self.head:
                self.head = None
                # 尾节点元素的情况
            else:
                pre.next = self.head
                # pre.next = cur.next

    def judgement(self):
        cur = self.head
        count = 1
        while cur != cur.next :
            cur = cur.next
            count += 1
            if count == 3:
                self.remove(cur.elem)
                print("%d-->"%cur.elem,end="")
                count = 0
        print(cur.elem)

if __name__ == '__main__':
    sll = SingleCycleLinkList()
    for i in range(1,42):
        sll.append(i)
    sll.judgement()

Published 31 original articles · won praise 13 · views 9910

Guess you like

Origin blog.csdn.net/qq_43497702/article/details/88047489