约瑟夫环的问题,python3实现

#encoding=utf-8

def josephus(n,m):

    “””

        环的问题,

        共有n个人围成一圈,从1开始报数,数到m时退出,再从1开始,直到所有人退出

    “””

    people = list(range(1,n+1))

    index = 0

    #给n个人编号放到表people中,从下标为0的人开始

    for i in range(n):

        #for循环用来控制内部循环执行次数

        count = 0

        while count < m:

            if people[index] != 0:

                count += 1

            if count == m:

                print(people[index],”退出”)

                #把退出的人的编号置0

                people[index] = 0

            index = (index+1) % n

    return

if __name__ == ‘__main__’:

    josephus(10,6)

    原文作者:约瑟夫环问题
    原文地址: https://blog.csdn.net/CrazyME0/article/details/79731323
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞