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. @余凯_西二旗民工 【SVM之菜鸟实现】—5步SVM

    #翻译#了下 余凯老师的 心法 以前的一篇博文:二分类SVM方法Matlab实现 前几日实现了下,虽然说是Linear-SVM,但是只要可以有映射函数也可以做kernel-svm function [ ...

  2. WIN32读写INI文件方法

      在程序中经常要用到设置或者其他少量数据的存盘,以便程序在下一次执行的时候可以使用,比如说保存本次程序执行时窗口的位置.大小.一些用户设置的 数据等等,在 Dos 下编程的时候,我们一般自己产生一个 ...

  3. iomanip

    在c++程序里面经常见到下面的头文件 #include <iomanip> io代表输入输出,manip是manipulator(操纵器)的缩写(在c++上只能通过输入缩写才有效.) 2作 ...

  4. javascript中的继承用法

    本文实例汇总了javascript关于继承的用法,希望本文所述对大家的javascript程序设计有所帮助.分享给大家供大家参考.具体如下:代码如下: /** * 实现子类继承父类,但不会产生多余的属 ...

  5. 如何完全退出android应用程序

    当一个android应用程序包含多个activity时,要完全退出android应用程序,便要销毁掉所有的activity,下面是一种网上流传的比较经典完美的方法: 首先要定义一个继承Applicat ...

  6. Apache调优

    1.调整文件描述符大小,默认Linux系统的文件描述符是1024,对于squid等一些服务来说                在高负载的情况下这些文件描述符是远远不够的,所以在部署该类服务器时修改文件 ...

  7. Java多线程——ThreadLocal类

    一.概述   ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个Thread,而是threadlocalvariable(线程局部变量).也许把它命名 ...

  8. webform repeater

    repeater:由模板构成,解析后模板就不存在了             需要指定数据源进行数据绑定 List<Fruit> list = new FruitDA().Select(); ...

  9. C# 面向对象 , 继承

    继承 class A { Console.WriteLine("hello world"); } class B:A { } 以上书写,表示B 是A 的子类,  B 同时继承A 中 ...

  10. Skin++ 皮肤库 CCheckListBox MFC 界面风格

    今天使用CCheckListBox,发现增加进去的字符串无法显示,但是当点击的时候,确有反应. 仔细检查代码,没有问题.之前也是这样用的,完全没有问题. 思前想后,觉得是因为使用了Skin++皮肤库, ...