Created
February 22, 2012 23:41
-
-
Save feynmanliang/1888417 to your computer and use it in GitHub Desktop.
Solution to 2s complement csfall11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Please write complete compilable code. | |
Your class should be named Solution | |
Read input from standard input (STDIN) and print output to standard output(STDOUT). | |
For more details, please check http://www.interviewstreet.com/recruit/challenges/faq/view#stdio | |
*/ | |
import java.util.Scanner; | |
public class Solution { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
int numCases = scan.nextInt(); | |
int[] output = new int[numCases]; | |
for (int i = 0; i < numCases; ++i) { | |
int solution = 0; | |
int a = scan.nextInt(), b = scan.nextInt(); | |
for (int j = a; j <= b; ++j) { | |
int bits = Integer.bitCount(j); | |
solution += bits; | |
} | |
output[i] = solution; | |
} | |
for (int line=0; line < numCases; ++line) | |
System.out.println(output[line]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment