Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the Joseph Ring problem with python

Shulou Source: shulou.com Published: 2022-06-03 05:43:46 10月03日 Update

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

Tags: Results personal tests questions Cerf code content round table articles ways methods time more knowledge articles links related output good concise Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Redmi Huawei macOS