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();
}
}
}
HDOJ 1390 Binary Numbers(进制问题)的更多相关文章
- HDOJ 1335 Basically Speaking(进制转换)
Problem Description The Really Neato Calculator Company, Inc. has recently hired your team to help d ...
- HDU-1390 Binary Numbers
http://acm.hdu.edu.cn/showproblem.php?pid=1390 Binary Numbers Time Limit: 2000/1000 MS (Java/Others) ...
- HDOJ(HDU) 2106 decimal system(进制相互转换问题)
Problem Description As we know , we always use the decimal system in our common life, even using the ...
- UVALive 3958 Weird Numbers (负进制数)
Weird Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/F Description Binary number ...
- PAT甲级——1100 Mars Numbers (字符串操作、进制转换)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90678474 1100 Mars Numbers (20 分) ...
- 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 ...
- hdoj 2031 进制转换
进制转换 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDOJ(HDU) 2097 Sky数(进制)
Problem Description Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=2 ...
- HDOJ(HDU) 1877 又一版 A+B(进制、、)
Problem Description 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数. Input 输入格式:测试输 ...
随机推荐
- 安装apache重启的时候,报错端口被占用,错误1
在cmd中执行以下命令来重新分配. netsh winsock reset. 还不行的话可以重启电脑,再不行就算apache配置文件错误.
- MediaPlayer 音频播放 示例
状态机.流程图.生命周期 对播放音频/视频文件和流的控制是通过一个状态机来管理的.下图显示一个MediaPlayer对象被支持的播放控制操作驱动的生命周期和状态. 椭圆代表MediaPlayer对象可 ...
- http方式的联调经验
这近在做一个项目,现在刚与厂家做联调.我的系统是windows2008,他的测试机是unix,他本机是XP的.本来用httppost方式给他本机发送中文数据是正常的,但是发送到他的unix上,中文就变 ...
- Tab标签栏 切换 权威总结
angular的标签栏,有两种方法实现: 内容全部加载到页面中,再利用ng-show指令. 将每一块要加载的内容做成模板,利用ng-if指令加载. 用bootstrap的tab组件 用angular的 ...
- YII框架路由和URL生成
路由和URL生成 当一个YII应用开始处理一个请求的时候,它首先要做的便是将请求的URL转化成一个路由.路由的作用是用于后续实例化相应的控制器和操作,以便处理请求,整个处理过程便叫做路由.路由的逆过程 ...
- Javascript预解析、作用域、作用域链
最近在看js的一些资料,总结一下昨晚看到的js作用域方面的知识,不准确的地方希望留言指正! 先看片段js代码如下: < script type="text/javascript&quo ...
- 关于在repeater中的checkbox实行多选和全选
今天项目中用到这一块,是一个b2b商城,业务是别人给客户留言后,客户从会员中心的留言管理中查看,用checkbox实行多选和全选后进行批量审核 首先在checkbox后加个hidden,作用见代码: ...
- iOS9中通过UIStackView实现类似大众点评中的效果图
效果图如下: 实现思路 整体可以看做为一个大的UIStackView(排列方式水平)包括一个子UIStackView(排列方式垂直),其中左边包括一个图片,右边的UIStackView中可以看做包括三 ...
- JS实现Tab切换
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 搭建ngrok服务器(ubuntu 14)-- 微信 80端口和IPC备案限制解决方案
概述: ngrok其实这东西,我也不是很懂,所以也直接跟大家说,这就是个类似花生壳的东西. 简单来说,它就好像把我们内网自己使用的电脑和服务器用vpn连接起来,然后你的电脑就可以从互联网来访问了,有个 ...