Children's games (the last remaining number in the circle)

Source: Internet
Author: User

Title Description:

Every year children's Day, Nowcoder will prepare some small gifts to visit the orphanage children, this year is also the case. HF as the senior veteran of the Nowcoder, naturally also prepared some small games. Among them, there is a game like this: first, let the children surround a big circle. Then, he randomly assigned a number m, so that the number of children numbered 0 began to count. Every time the child shouted to M to sing a song, and then can be in the Gift box arbitrary selection of gifts, and no longer back to the circle, from his next child, continue to 0...m-1 count off .... Go on like this .... Until the last child left, can not perform, and get Nowcoder valuable "Detective Conan" Collector's Edition (limited number of seats Oh!!) ^_^). Please try to think, which one of the children will get the present?

Solution One:

Ideas:

Find the rules. First, the last number in the first n numbers (0,1,..., n-1) is defined as the equation for N and M is f (n,m). In these n numbers, the first number to be deleted is (m-1)%n, which is k for simplicity.
* Then the number of n-1 left after the deletion of K is 0,1,..., k-1,k+1,..., n-1, and the next number to start counting is k+1. Equivalent to the remainder of the sequence, k+1 to the front, resulting in sequence k+1,..., n-1,0,... k-1.
* The last number remaining in the sequence should also be a function of N and M. Since the sequence is not the same as the original sequence (the initial sequence is a sequential sequence starting at 0), the function differs from the previous function and is recorded as F ' (n-1,m). The last remaining number of the initial sequence F (n,m) must be the last remaining number of the remaining sequence F ' (n-1,m), so f (n,m) =f ' (n-1,m). Next we take the remainder of this n-1 number sequence k+1,..., n-1,0,... k-1 as a map, the result of which is a sequence from 0 to n-2:

K+1-0
K+2-1
...
N-1-N-k-2
0-N-k-1
...
K-1-N-2

Define the map as P, then p (x) = (x-k-1)%n, the corresponding inverse map is p-1 (x) = (x+k+1)%n,f ' (n-1,m) = P-1 [F (n-1,m)]=[f (n-1,m) +k+1]%n. Substituting k=m%n-1 for f (n,m) =f ' (n-1,m) =[f (n-1,m) +m]%n.

We represent this relationship as:

0 N=1
F (n,m) ={
[F (n-1,m) +m]%n n>1

 Public intLastremaining_solution (intNintm) {if(n<1| | M<1)            return-1; if(m>=N)return-1; intLast=0;  for(inti=2;i<=n;i++) { last= (last+m)%i; }        returnLast ; }

Solution Two:

 /** Using an array to simulate the ring, the idea is still relatively simple, but the various subscript to clarify*/     Public Static intFindlastnumber (intNintm) {        if(n<1| | M&LT;1)return-1; int[] Array =New int[n]; inti = -1,step = 0, Count =N;  while(count>0) {//the last element is also set to 1 when jumping out of a loopi++;//points to the next element of the previous deleted object.             if(i>=n) i=0;//analog ring.             if(Array[i] = =-1)Continue;//skips the object that was deleted. step++;//The record has gone through.             if(step==m) {//Locate the object you want to delete. Array[i]=-1; Step= 0; Count--; }                }        returnI//returns the I, which is the last element set to 1, when jumping out of a loop}

Solution Three:

 Public intLastremaining_solution (intNintm) {        if(m = = 0 | | n = = 0) {            return-1; } ArrayList<Integer> data =NewArraylist<integer>();  for(inti = 0; I < n; i++) {//Add all the numbers to an arrayData.add (i); }        intindex =-1;  while(Data.size () > 1) {//System.out.println (data);Index = (index + m)%data.size ();//System.out.println (Data.get (index));Data.remove (index); index--;Why is the index minus 1 here? , because at the beginning of the index is-1, itself index+m represents the number of M, when the index number is deleted, Index-1+m still represents the next number of M

}
return data.get (0); }

Children's games (the last remaining number in the circle)

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.