Pat-java-5-28 Monkey Selection King (20 points)

Source: Internet
Author: User

A group of monkeys will choose the new Monkey King. The new Monkey King's choice is: Let n only the candidate monkeys in a circle, from a position from a sequence numbered 1~n. Starting from the 1th number, each round from 1 to report 3, where the report 3 of the monkeys are out of the circle, and then from the next close to a monkey start the same count. So constantly circulating, the last one left is chosen as the Monkey King. What is the original number of monkeys elected Monkey King?

Input format:

The input gives a positive integer n (\le≤1000) in a row.

Output format:

Prints the number of the selected monkey in a row.

Input Sample:
11
Sample output:
7

This problem is similar to Joseph Ring.
Joseph Ring Problem: a circle of n individuals, began to count, the person to report m suicide, and then restart the count, ask the last person who committed suicide?
The most common way is to simulate the entire process: Build a bool array, true indicates that this person is still alive, false means that he has committed suicide. the entire process can be simulated by simulating the entire process , with the complexity of O (NM). Mathematical methods can be used to solve:

To re-describe the problem: N individuals (number 0~ (N-1)), starting from 0 count, reporting (M-1) suicide, the remainder continues to count off from 0. Ask for the number of the last person who committed suicide.

n the personal number is as follows:


01 2 3 ... ··· N-3 N-2 N-1


The first person to commit suicide is (M-1)%n,41 Personally, the person reporting 3 commits suicide, then the number of a person who commits suicide is (3-1)%41=2. Number (M-1)%n after committing suicide, the remainder are arranged as follows:

01 ... M-2 M ... ··· N-2 N-1
After someone has committed suicide, the next position M is again from zero, so the ring should be as follows:

Mm+1 ... N-1 0 1 2 ... ··· M-2
Renumber the above-listed order:

Mm+1 ... N-1 N-10 1 ... ··· M-2 0 1 ... N-2 The problem becomes (N-1) individual, the person who report for (M-1) commits suicide, the problem scale reduces. This goes on and on, with the last number of people numbered 0. Represented by a function:

F (1) =0

Who was the last person to commit suicide when there were 2 people (n=2), who reported committing suicide (M-1)? Should be in only one person, count off when the final suicide of the serial number plus m, because the person who report M-1 has committed suicide, only 2 people, and another suicide is the last suicide, using the function to express:

F (2) =f (1) +m

You can get a recursive formula:

F (i) =f (i-1) +m

Because it may exceed the total number of people, it is required to model

f (i) = (f (i-1) +m)%i

With a recursive formula, you can find the results in O (N) time.

#include <iostream>  using namespace std;  int main ()  {      int n;//total number of people      int m;//interval How many individuals        cin>>n;      cin>>m;      int result=0;//n=1 case for      (int i=2; i<=n; i++)      {          result= (result+m)%i;      }      cout<< "The last person to commit suicide is:" <<result+1<<endl;//result to add 1      return 0;  }  

The code for the problem is as follows:
Import Java.util.scanner;public class main{public static void Main (string[] args) {Scanner sc = new Scanner (system.in); int n = sc.nextint (); System.out.println (f (n) +1);} public static int F (int n) {if (n==1) {return 0;} Else{return (f (n-1) +3)%n;}}}






Pat-java-5-28 Monkey Selection King (20 points)

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.