HDOJ 1390 Binary Numbers(进制问题)

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’s in the binary representation of 13 are 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 integer d equal to the number of data sets, 1 <= d <= 10. The data sets follow.

Each data set consists of 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

思路:

就是输入一个数n,n二进制假如为m。

就是输出二进制m这个数的1所在的位数。从小到大输出。

例如:输入13.

13的二进制数是1101;

所以输出为:0 2 3

注意,最后一个数字后面没有接空格。

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int n =sc.nextInt();
            String nstr = Integer.toString(n, 2);
            //System.out.println(nstr);
            boolean isOne=true;
            for(int i=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();

        }
    }

}
时间: 2024-07-28 20:50:48

HDOJ 1390 Binary Numbers(进制问题)的相关文章

HDU 1390 Binary Numbers

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1390 题目描述: Binary Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2834    Accepted Submission(s): 1758Problem Description Given a positive

HDOJ 1335 Basically Speaking(进制转换)

Problem Description The Really Neato Calculator Company, Inc. has recently hired your team to help design their Super Neato Model I calculator. As a computer scientist you suggested to the company that it would be neato if this new calculator could c

杭电 HDU ACM 1390 Binary Numbers

Binary Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3372    Accepted Submission(s): 2026 Problem Description Given a positive integer n, find the positions of all 1's in its binary r

HDU 1390 Binary Numbers(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1390 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's in the binary

Bailian2973 Skew数【进制】

2973:Skew数 总时间限制: 1000ms 内存限制: 65536kB 描述 在 skew binary表示中, 第 k 位的值xk表示xk(2k+1-1). 每个位上的可能数字是0 或 1,最后面一个非零位可以是2, 例如, 10120(skew) = 1(25-1) + 0(24-1) + 1(23-1) + 2(22-1) + 0(21-1) = 31 + 0 + 7 + 6 + 0 = 44. 前十个skew数是 0.1.2.10.11.12.20.100.101.以及102. 输

HDU1887 ZOJ2982 UVALive3958 Weird Numbers【进制】

Weird Numbers Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 594 Accepted Submission(s): 185 Problem Description Binary numbers form the principal basis of computer science. Most of you have hear

ACM--26进制加法--HDOJ 2100--Lovekey--大数--字符串处理

HDOJ题目地址:传送门 Lovekey Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7783    Accepted Submission(s): 2519 Problem Description XYZ-26进制数是一个每位都是大写字母的数字. A.B.C.-.X.Y.Z 分别依次代表一个0 ~ 25 的数字,一个 n 位的26

ZOJ1154 Niven Numbers【进制】

Niven Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB A Niven number is a number such that the sum of its digits divides itself. For example, 111 is a Niven number because the sum of its digits is 3, which divides 111. We can also specify a numb

hdoj 2031 进制转换

进制转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30304    Accepted Submission(s): 16811 Problem Description 输入一个十进制数N,将它转换成R进制数输出. Input 输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<&