Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the Joseph Ring problem with python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report