Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

纪念60题,没用位运算,算层数水过。。。

Posted by mid_winter at 2012-12-04 16:47:44 on Problem 2309
import java.util.Scanner;

public class BST {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		while (N-- > 0) {
			int input = sc.nextInt();
			int allLevel = (int) (Math.log(input) / Math.log(2.0));
			int root = (int) Math.pow(2.0, allLevel);

			int level = 0;
			while (level < allLevel) {
				if (root == input) {
					break;
				}

				if (root > input) {
					root -= (int) Math.pow(2.0, allLevel - level - 1);
					level++;
				}

				if (root < input) {
					root += (int) Math.pow(2.0, allLevel - level - 1);
					level++;
				}
			}

			int min = input - (int) Math.pow(2.0, allLevel - level) + 1;
			int max = input + (int) Math.pow(2.0, allLevel - level) - 1;

			System.out.printf("%d %d%n", min, max);
		}
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator