HDOJ 1390 binary Numbers (binary issue)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.