The Python and C + + solutions for the Joseph problem

Source: Internet
Author: User
Is it Joseph's problem?
Joseph question is an interesting math game, the rules of the game are as follows:
1, n individuals in a circle, numbering starting from 1, and then to N.
2, the game participants numbered m began to count off, count off from 1 onwards, behind the people to count Solitaire, until K, counted as K will be out of the people.
3. The next player of the exit is then counted off from 1, so loop until the end of the game when the player is left, this player is the game winner.
So the question is, which number is the game winner?
Here are some simple lines of Python code to solve this problem:

#!/usr/bin/env Python # joseph Problem def Joseph (Total, begins, count):   queue = range (1, total + 1)   death = (begi NS + count-2)% len (queue)   for times in Range (TOTAL-1):     print ' out: ', Queue[death]     del Queue[death]     D Eath = (death + count-1)% len (queue)   

In the Joseph () function, the parameter total is the above mentioned N,begins namely M,count and K, each cycle count out a number, the last remaining survivor is the game winner.

The usual implementation of C + + is as follows:

#include 
 
  
   
  using namespace std;void main () {int n=0,c=0; cout<< "Please enter the number of people:n="; CI n>>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
  
   <><"->
   ";  }  else  {  cout<
   <>
   < pre="">
    
   

This is common in the C + + language test, the following program to enter the number of people from the console n,c and the elimination of the queue's personnel number sequentially output to the console.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.