Problem Description
Given a positive integer n, find the positions of all 1 ' s in its binary representation. The position of the least significant bit is 0.
Example
The positions of 1 ' in the binary representation of 0, 2, 3.
Task
Write a program which for each data set:
Reads a positive integer n,
Computes the positions of 1 s in the binary representation of N,
Writes the result.
Input
The first line of the input contains exactly one positive an integer d equal to the number of data sets, 1 <= d <= 10. The data sets follow.
Each data set consists the exactly one line containing exactly one integer n, 1 <= n <= 10^6.
Output
The output should consists of exactly d lines, one line for each data set.
Line I, 1 <= i <= D, should contain increasing sequence of integers separated by single spaces-the positions of 1 ' s in the binary representation of the i-th input number.
Sample Input
1
13
Sample Output
0 2 3
Ideas:
is to enter a number n,n binary if it is M.
Is the number of bits of the 1 that the output binary m is located in. From small to large output.
For example: Enter 13.
The binary number of 13 is 1101;
So the output is: 0 2 3
Note that the last number is not followed by a space.
Import Java.util.Scanner; Public classmain{ Public Static void Main(string[] args) {Scanner SC =NewScanner (System.inch);intt = Sc.nextint (); while(t-->0){intn =sc.nextint (); String nstr = integer.tostring (n,2);//system.out.println (NSTR);Boolean isone=true; for(intI=nstr.length ()-1; i>=0; i--) {if(Nstr.charat (i) = =' 1 '){if(Isone) {System. out. Print (Nstr.length ()-1-I.); Isone=false; }Else{System. out. Print (" "+ (Nstr.length ()-1-i)); }}} System. out. println (); } }}
HDOJ 1390 binary Numbers (binary issue)