Algorithm and data structure surface question (22)-Find the last number left in this circle __java

Source: Internet
Author: User
Tags data structures
Topics


N Numbers (0,1,..., n-1) Form a circle, starting with the number 0, each time the number of m digits is removed from the Circle
(the first is the current number itself, the second is the next number of the current number). When a number is deleted,
the next number from the deleted number continues to delete the first m digits. Find the last number left in this circle.


Reference Articles


http://blog.csdn.net/lzj509649444/article/details/7056742


Personal Understanding


The first way of thinking


1.m=8 Removal Process
















2.m=4 the removal process






Analysis


After K-delete, we are looking for the number in the index=5, but after k+1 the deletion of the number we are looking for has become index=18. This change in index has nothing to do with M and the remaining number N.

Delete the number of M numbers, there are 3 kinds of cases:


the number of indexed values to delete


1.m>n, traversing more than one lap, this time removing the element for the first m%n

2.m<n, traversing no more than one lap, this time the deleted element is the first M

3.m=n, the deleted element is the No. 0 one.


The index of the number of deletions in summary can be represented by m%n.


the relationship between the F (n) and F (n-1) times


K Delete, set the number of the index to find the value of I. is f (n)

K+1 Delete, the index value to find is J. is f (n-1)


There are 2 kinds of situations:

1. The number of deleted index values is greater than I. namely M%n>i


j = N-m%n+i
18 = 21-8%21+5

2. The number of deleted index values is less than I. namely M%n<i


j = i-m%n
1=5-4%21


Because I must be less than N. So i = i%n


Summary




However, the above formula contains two cases, we now know the condition is F (0) position, and cannot get the value of f (n), so the above formula deduction failed.


The second way of thinking


Because we ask for the last number of values, then we ask for the remaining number in the original set index value, we can get the number by index-1.


So the formula goes upside down and becomes when f (0) = 0. For the problem of F (n), so we want to look at the above diagram from backward to forward, how to deduce the position of the previous graph according to the position of the next graph.


if M=8


After the K-deletion:

At this point n=20, the remaining number at the current position index value is 18,

Then after the K-1 Delete:

N=21, the rest of the number in what position. In the figure above, it is clear that the number is in the 5 position. Can you find a recursive formula? Specific reasoning process. The above reference article has the inference process, we need to do is verify is correct.



If f (20,8) is 18,m=8,n=21, then

F (21,8) = (18+8)%21 = 5.

If f (20,4) = 1,m=4,n=21, then

F (21,4) = (1+4)%21 = 5.


Other superfluous validations are not done.


Code


public int Getlastindexbyonline (int n, int m) {
		//n=0, empty set. M=0, do not delete
		if (N < 1 | | m < 1) {
			return-1;
		}
		int last = 0;
		When N=1, return is the subscript is 0, when the n>=2 is necessary to do loop traversal for
		(int i = 2; I <= n; i++) {Last
			= (last + m)% i;
		}
		return last;
	}

	public static void Main (string[] args) {
		//TODO auto-generated method stub
		int n = +;
		int m = 8;
		Problem18 problem = new Problem18 ();
		int index = Problem.getlastindexbyonline (n, m);
		System.out.println ("Last number is:" + index);
	}

Output


The last number is: 0


Summary


The problem is not to examine your code capabilities, nor to examine your data structures and algorithms. It's your ability to deduce.



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.