NY-63-monkey's whereabouts

Source: Internet
Author: User

 

Monkey fall time limit: 3000 MS | memory limit: 65535 KB
Difficulty: 3
Description

There is a binary tree with a maximum depth of D, and the depth of all leaves is the same. The numbers of all nodes from left to right are 1, 2, 3,..., and 2 minus 1 to the power of D. Put a monkey at Node 1 and it will run down. Each inner node has a switch that is initially closed. When a monkey runs to a switch, its status changes. When it reaches an inner node, if the switch is closed, the monkey moves to the left. Otherwise, the monkey moves to the right until it reaches the leaf node.

Some monkeys start to run down from node 1. Where will the last monkey go?

 

Input

Enter the depth D of the binary tree leaves, and the number of monkeys I. Assume that I cannot exceed the number of leaves of the entire tree. D <= 20. The end ends with 0.
Output
Output the leaf number of the monkey I.
Sample Input
4 23 40 0
Sample output
127

Code:

 

# Include "stdio. h "int main () {int d, I, j, num = 1; while (scanf (" % d ", & d, & I ), d & I) {num = 1; for (j = 1; j <d; j ++) {if (I % 2 = 0) num = num * 2 + 1; // turn an even number to the right. Turn an odd number to the left. else num = num * 2; I = (I + 1)/2; // avoid I = 0} printf ("% d \ n", num);} return 0 ;}

 

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.