Interview topic: The problem of cat eating mice

Source: Internet
Author: User

A few days ago to a foreign company in Shanghai to participate in the written test, because the examination is tight, some of the major problems can not be completed, is very depressed. Now we are going to discuss one of the questions in this article---the problem of cat eating mice. Write this article just want to communicate with you and learn, inevitably there will be mistakes and deficiencies, hope to get everyone's criticism, in this we are grateful!

I. Description of the problem

The existing n mice in a circle, a cat from any place to eat mice, every time a mouse to eat, please give the last mouse number? The title requirement is to give the mouse number n, output the cat last eat the mouse number.

Second, problem solving

Let's say the mouse is numbered clockwise, and the cat starts eating from the first mouse. For example, the existing 4 mice around a circle, the cat should eat the mouse order should be 1->3->2->4, that is, the last one to eat the number of mice is 4.

Program Design Idea Description:

The cat moves from the mouse array from the beginning, and if it touches the mouse and the interval sign is 1, then eat the mouse, then the interval sign is 0, the remaining mouse number minus 1, continue to move backwards; if you encounter a mouse but the interval sign is 0, then don't eat the mouse, the interval sign is 1, then move backwards; if you don't touch the mouse, move back. ; If you move to the end of the array, start from scratch to make the circle loop.

The mouse array, Iparray, was used to indicate whether or not a mouse was present in a particular location, with 1 indicating a mouse presence and 0 indicating that the mice had been eaten.

The interval sign Ijian is 1, which means that if you run into a mouse, you can eat it, or 0, if you have just eaten a mouse, you should eat it every time, and then hit the next mouse on the interval sign 1, but do not eat mice.

The remaining mice were iyu after each mouse was eaten; when the remaining mouse count is 1 o'clock, locate the mouse directly and output its number, that is, the array subscript value plus 1, to the end of the program. The implementation can refer to the source code

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.