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(); }
} }

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

  1. HDOJ 1335 Basically Speaking(进制转换)

    Problem Description The Really Neato Calculator Company, Inc. has recently hired your team to help d ...

  2. HDU-1390 Binary Numbers

    http://acm.hdu.edu.cn/showproblem.php?pid=1390 Binary Numbers Time Limit: 2000/1000 MS (Java/Others) ...

  3. HDOJ(HDU) 2106 decimal system(进制相互转换问题)

    Problem Description As we know , we always use the decimal system in our common life, even using the ...

  4. UVALive 3958 Weird Numbers (负进制数)

    Weird Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/F Description Binary number ...

  5. PAT甲级——1100 Mars Numbers (字符串操作、进制转换)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90678474 1100 Mars Numbers (20 分) ...

  6. CodeForces-1249C2-Good Numbers (hard version) -巧妙进制/暴力

    The only difference between easy and hard versions is the maximum value of n. You are given a positi ...

  7. hdoj 2031 进制转换

    进制转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. HDOJ(HDU) 2097 Sky数(进制)

    Problem Description Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=2 ...

  9. HDOJ(HDU) 1877 又一版 A+B(进制、、)

    Problem Description 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数. Input 输入格式:测试输 ...

随机推荐

  1. python常用的一些东西——sys、os等

    1.常用内置函数:(不用import就可以直接使用)      help(obj) 在线帮助, obj可是任何类型     callable(obj) 查看一个obj是不是可以像函数一样调用     ...

  2. Spring-----1、Spring一个简短的引论

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVrZXdhbmd6aQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  3. [转] DAG算法在hadoop中的应用

    http://jiezhu2007.iteye.com/blog/2041422 大学里面数据结构里面有专门的一章图论,可惜当年没有认真学习,现在不得不再次捡 起来.真是少壮不努力,老大徒伤悲呀!什么 ...

  4. java socker编程

    转自http://haohaoxuexi.iteye.com/blog/1979837 对于Java Socket编程而言,有两个概念,一个是ServerSocket,一个是Socket.服务端和客户 ...

  5. c# 字符串编码问题

    一. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte). ...

  6. js 获取元素在页面上的偏移量的最佳方式

    使用js制作效果时,我们常常要获取某个元素在页面上的偏移量(例如tip提示框功能).而获取偏移量可以直接获取相对于document的偏移量,也可以获取相对与视口的偏移量(viewpoint)加上页面滚 ...

  7. MySQL 数据表修复及数据恢复

    1. MYSQL数据表在什么情况下容易损坏? 服务器突然断电导致数据文件损坏. 强制关机,没有先关闭mysql 服务等.   2. 数据表损坏后的主要现象是什么? 从表中选择数据之时,得到如下错误:I ...

  8. git add相关

    git add -A stages All git add . stages new and modified, without deleted git add -u stages modified ...

  9. MySQL 行子查询

    MySQL 行子查询 行子查询是指子查询返回的结果集是一行 N 列,该子查询的结果通常是对表的某行数据进行查询而返回的结果集. 一个行子查询的例子如下: SELECT * FROM table1 WH ...

  10. nat123 与微信公众号开发者测试账号配合调试

    由于公司本身是做互联网 电商行业的,微信也是一个大块,近期开始花费时间在整合,总结自己的经验,看看之前的实现是否有明显的问题. 花了点钱(8块钱)充值了nat123,进行了内网穿透.之前也有使用花生壳 ...