我用Java学算法——UVa679-Dropping Balls

一.题目描述

A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each time the ball being dropped first visits a non-terminal node. It then keeps moving down, either follows the path of the left subtree, or follows the path of the right subtree, until it stops at one of the leaf nodes of FBT. To determine a ball’s moving direction a flag is set up in every non-terminal node with two values, either false or true. Initially, all of the flags are false. When visiting a non-terminal node if the flag’s current value at this node is false, then the ball will first switch this flag’s value, i.e., from the false to the true, and then follow the left subtree of this node to keep moving down. Otherwise, it will also switch this flag’s value, i.e., from the true to the false, but will follow the right subtree of this node to keep moving down. Furthermore, all nodes of FBT are sequentially numbered, starting at 1 with nodes on depth 1, and then those on depth 2, and so on. Nodes on any depth are numbered from left to right.
For example, Fig. 1 represents a fully binary tree of maximum depth 4 with the node numbers 1, 2, 3, …, 15. Since all of the flags are initially set to be false, the first ball being dropped will switch flag’s values at node 1, node 2, and node 4 before it finally stops at position 8. The second ball being dropped will switch flag’s values at node 1, node 3, and node 6, and stop at position 12. Obviously, the third ball being dropped will switch flag’s values at node 1, node 2, and node 5 before it stops at position 10.
在这里插入图片描述
Fig. 1: An example of FBT with the maximum depth 4 and sequential node numbers.

Now consider a number of test cases where two values will be given for each test. The first value is D, the maximum depth of FBT, and the second one is I, the I-th ball being dropped. You may assume the value of I will not exceed the total number of leaf nodes for the given FBT.
Please write a program to determine the stop position P for each test case.
For each test cases the range of two parameters D and I is as below:
2 ≤ D ≤ 20, and 1 ≤ I ≤ 524288.

Input
Contains l + 2 lines.
Line 1 l the number of test cases
Line 2 D1 I1 test case #1, two decimal numbers that are separated by one blank

Line k + 1 Dk Ik test case #k Line l + 1 Dl Il test case #l
Line l + 2 -1 a constant ‘-1’ representing the end of the input file

Output
Contains l lines.
Line 1 the stop position P for the test case #1

Line k the stop position P for the test case #k

Line l the stop position P for the test case #l

Sample Input
5
4 2
3 4
10 1
2 2
8 128
-1

Sample Output
12
7
512
3
255

二.解题思路

  • 方法一:
    根据题目描述,每次小球落下后,开关的状态就发生改变,所以首先需要一个用于保存开关状态的boolean数组sta[],我们将索引赋予的语义是小球的编号k,开关状态为关(0),小球向左走,下一个球的编号就是k * 2,开关状态为开(1),小球向右走,下一个球的编号为2 * k+1。最后,如果算出的下一个小球的编号k>总结点数,退出并打印最后小球的编号k/2。
  • 方法二:
    第一种方法如果小球总数较大就会浪费大量时间,空间。现在有个方法可以之间算出所求的小球编号。首先前两个下落的小球一定是第一个向左,另一个向右,所以可以根据小球总数I判断,如果I是奇数,在左子树且,反之在右子树。例如:输入3 3,那么I=3是奇数,首先判断小球在左子树,第一次小球向左走,k = k * 2 = 2,它是走左子树的第(I+1)/2个小球,I = (I+1)/2 = 2;第二次小球向右走,k = k * 2 + 1 = 5,它是走左子树的第I/2个小球,I = I/2 = 1。因此输出5。

三.解题方法

  • 方法一:
import java.util.Scanner;

public class DroppingBalls {

	public static void main(String[] args) {
		boolean[] sta; // 开关的状态
		Scanner in = new Scanner(System.in);
		while (!in.hasNext("-1")) {
			String l = in.nextLine();
			String[] n = l.split(" ");
			if(n.length > 1){
			int D = Integer.parseInt(n[0]); // 树的深度
			int I = Integer.parseInt(n[1]); // 放下小球的总数
			
				int max = (int) Math.pow(2, D);
				sta = new boolean[max];
				int k = 1; // 小球的编号
				for (int i = 0; i < I; i++) {
					k = 1;
					for (;;) {
						sta[k] = !sta[k];
						k = sta[k] ? k * 2 : k * 2 + 1;
						if (k > max - 1)
							break;
					}
				}
				System.out.println(k / 2);
			}
		}
		in.close();
	}

}

  • 方法二:
import java.util.Scanner;

public class DroppingBalls2 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while (!in.hasNext("-1")) {
			String l = in.nextLine();
			String[] n = l.split(" ");
			if (n.length > 1) {
				int D = Integer.parseInt(n[0]); // 树的深度
				int I = Integer.parseInt(n[1]); // 放下小球的总数

				int k = 1; // 小球的编号
				for (int i = 0; i < D - 1; i++) {
					if (I % 2 == 0) { // I是偶数,小球再根节点的右子树中,它是该子树中往右走的第I/2个球
						k = k * 2 + 1;
						I /= 2;
					} else {
						k *= 2;
						I = (I + 1) / 2;
					}
				}
				System.out.println(k);
			}
		}
		in.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值