search
HomeBackend DevelopmentPython Tutorial约瑟夫问题的Python和C++求解方法

么是约瑟夫问题?
约瑟夫问题是一个有趣的数学游戏,游戏规则如下:
1、N个人围成一个圈,编号从1开始,依次到N。
2、编号为M的游戏参与者开始报数,报数从1开始,后面的人报数接龙,直到K为止,报数为K的人将出局。
3、出局者的下一个玩家接着从1开始报数,如此循环,直到剩下一个玩家时游戏结束,这个玩家就是游戏获胜者。
那么问题来了,哪个编号是游戏获胜者呢?
下面通过简单的几行python代码来解决这个问题:

#!/usr/bin/env python 
# Joseph Problem 
def joseph(total, begins, count): 
  queue = range(1, total + 1) 
  death = (begins + count - 2) % len(queue) 
  for times in range(total - 1): 
    print 'out: ', queue[death] 
    del queue[death] 
    death = (death + count -1) % len(queue) 
  print 'survivor: ', queue[0] 

joseph()函数中,参数total即上面提到的N,begins即M,count及K,每次循环报数out一个编号,最后剩下的survivor便是游戏获胜者。

而C++的通常实现方法如下:

#include <iostream>
using namespace std;
void main()
{
 int N=0,C=0;
 cout<<"Please enter the number of people:N=";
 cin>>N;
 cout<<"Please enter:C=";
 cin>>C;
 int i=0,j=0,n=N,s=0;
 int *a=new int [N];
 for (i=0;i<N;i++)
 {
 a[i]=1;
 }

 while(0!=n)
 {
 s+=a[j];
 if(C==s)
 {
  a[j]=0;
  s=0;
  --n;
  if(0!=n)
  {
  cout<<j+1<<"->";
  }
  else
  {
  cout<<j+1<<endl;
  }
 }
 j=(j+1)%N;
 }
 delete []a;
}

这是C++语言常见的机试题目,以下程序实现从控制台输入人数N,C并将剔除出队列的人员编号按顺序输出到控制台上。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are Exception Groups in Python?What are Exception Groups in Python?Apr 30, 2025 pm 02:07 PM

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

What are Function Annotations in Python?What are Function Annotations in Python?Apr 30, 2025 pm 02:06 PM

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

What are unit tests in Python?What are unit tests in Python?Apr 30, 2025 pm 02:05 PM

The article discusses unit tests in Python, their benefits, and how to write them effectively. It highlights tools like unittest and pytest for testing.

What are Access Specifiers in Python?What are Access Specifiers in Python?Apr 30, 2025 pm 02:03 PM

Article discusses access specifiers in Python, which use naming conventions to indicate visibility of class members, rather than strict enforcement.

What is __init__() in Python and how does self play a role in it?What is __init__() in Python and how does self play a role in it?Apr 30, 2025 pm 02:02 PM

Article discusses Python's \_\_init\_\_() method and self's role in initializing object attributes. Other class methods and inheritance's impact on \_\_init\_\_() are also covered.

What is the difference between @classmethod, @staticmethod and instance methods in Python?What is the difference between @classmethod, @staticmethod and instance methods in Python?Apr 30, 2025 pm 02:01 PM

The article discusses the differences between @classmethod, @staticmethod, and instance methods in Python, detailing their properties, use cases, and benefits. It explains how to choose the right method type based on the required functionality and da

How do you append elements to a Python array?How do you append elements to a Python array?Apr 30, 2025 am 12:19 AM

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware

How do you debug shebang-related issues?How do you debug shebang-related issues?Apr 30, 2025 am 12:17 AM

The methods to debug the shebang problem include: 1. Check the shebang line to make sure it is the first line of the script and there are no prefixed spaces; 2. Verify whether the interpreter path is correct; 3. Call the interpreter directly to run the script to isolate the shebang problem; 4. Use strace or trusts to track the system calls; 5. Check the impact of environment variables on shebang.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools