Ball Drop:
Full binary tree, max depth d. All nodes are numbered from top to bottom from left to right 1,2,3...2 D-1. Place a small ball at node 1 and it will fall. Each inner node is powered by a switch that initially shuts down and its state changes every time a ball falls on a switch. When the ball reaches an inner node, if the switch on the node is closed, go left, or go right until the leaf node is traversed.
problem, input depth and number of balls
Output Node number
Input 4 2 Output 12
Input 3 4 Output 7
Reference implementations
function f (depth,count) {var states = new Array (), Var r;for (var i = 0;i < count;i++) {for (var root = 1;root <math.pow ( 2,DEPTH-1);) {if (States[root] && states[root].open) {States[root].open = False;root = root * 2 + 1;} Else{if (!states[root]) {states[root]={open:true};} else if (States[root].open = = False) {States[root].open = true;} root = root * 2;} R=root;}} return r;} Console.log (f (4,2)), Console.log (f (3,4)), Console.log (f (10,1)), Console.log (f (2,2)), Console.log (f (8,128)); Console.log (f (16,12345));
Algorithm exercise--Small ball drop