Binary Numbers
时空限制
Time Limit:1000ms
Resident Memory Limit:1024KB
Output Limit:1024B
题目内容
Given a positive integer n, print out the positions of all 1’s in its binary representation. The position of the least significant bit is 0.≤d
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 follows. Each data set consists of exactly one line containing exactly one intger n, 1≤n≤106.
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.
题目来源
Central Europe 2001, Practice
解题思路
本题主要考查十进制到二进制的转换。十进制整整数转换为二进制整数的方法是不断除2取余,而被除数则不断除2取整,知道被除数变为0.把各位余数按相反的顺序连接起来,正好是该整整数的二进制表示。
本题要求打印一个正整数的二进制中1的位置,所以,只要把每位二进制位存入向量中即可,最后,再在向量中进行处理,这样很省事。
本题格式上,要求两位输出之间用一个空格隔开,而每行的最后不能有空格。这点要特别注意。
流程分析
(1)定义一个整型向量容器存储整数对应的二进制;定义一个整型变量作为输入整数的个数;定义一个整型变量作为每次输入的整数;定义一个整型变量作为是否输出空格的标志
(2)从键盘读入输入的整数个数
(3)对输入的每个整数,先将向量容器清空,
(4)对输入的每个整数,对其每次除2的商,不断对2取余并将取余结果插入向量容器
(5)对输入的每个整数,对其二进制的每一位,首先判断是不是第一次输出,若是则直接输出其位置,即在向量容器中的下标;若不是第一次输出,则先输出空格再输出其位置
参考代码
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc,char * argv[])
{
vector<int> v;
int n,a;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a;
v.clear();
for(int j=a;j;j=j/2)
{
v.push_back(j%2?1:0);
}
int p=0;
for(int k=0;k<v.size();k++)
{
if(v[k]==1)
{
if(p==0) cout<<k;
else cout<<" "<<k;
p=1;
}
}
cout<<endl;
}
system("pause");
return 0;
}
运行结果
Binary Numbers的更多相关文章
- HDU-1390 Binary Numbers
http://acm.hdu.edu.cn/showproblem.php?pid=1390 Binary Numbers Time Limit: 2000/1000 MS (Java/Others) ...
- Binary Numbers(HDU1390)
Binary Numbers 点我 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum
E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...
- zoj 1383 Binary Numbers
Binary Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB Given a positive integer n, print o ...
- 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...
- LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)
1022. 从根到叶的二进制数之和 1022. Sum of Root To Leaf Binary Numbers 题目描述 Given a binary tree, each node has v ...
- [Swift]LeetCode1022. 从根到叶的二进制数之和 | Sum of Root To Leaf Binary Numbers
Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary number ...
- Binary Numbers AND Sum CodeForces - 1066E (前缀和)
You are given two huge binary integer numbers aa and bb of lengths nn and mmrespectively. You will r ...
- 【leetcode】1022. Sum of Root To Leaf Binary Numbers
题目如下: Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary n ...
- 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
随机推荐
- name是个特殊的变量名吗
这是为什么?求大神
- python 安装pycurl
yum install libghc-gnutls-dev opnssl nss -y 126 yum install libghc-gnutls-dev -y 127 yum install htt ...
- 【css】绝对定位的元素在 ie6 下不显示
问题描述: 在 ie6 中如果一个浮动元素与绝对定位元素相邻的话,在某些情况下绝对定位元素将会消失. 产生原因: 只有当绝对定位元素的邻近浮动元素的宽度大于父层宽度减 3 时(即如果父层宽度是 300 ...
- C#内置泛型委托:Func委托
1.什么是Func委托 Func委托代表有返回类型的委托 2.Func委托定义 查看Func的定义: using System.Runtime.CompilerServices; namespace ...
- 【Python】 linecache模块读取文件
[linecache] 过往在读取文件的时候,我们通常使用的是这种模式: with open('file.txt','r') as f: line = f.readline() while line: ...
- PCL点云配准(2)
(1)正态分布变换进行配准(normal Distributions Transform) 介绍关于如何使用正态分布算法来确定两个大型点云之间的刚体变换,正态分布变换算法是一个配准算法,它应用于三维点 ...
- Linux gcc/g++链接编译顺序详解
gcc/g++链接时对库的顺序要求 -Ldir Add directory dir to the list of directories to be searched for -l. -llibrar ...
- Android 软键盘弹出与关闭监听
private void listenerSoftInput() { final View activityRootView = findViewById(R.id.activityRoot); ac ...
- PHP + Smarty + html5 构建Wap应用
一 简介 Smarty是一个PHP编写的模板引擎(template engine),主要用于构建web应用程序的表示层.Smarty的主页是http://www.smarty.net/down ...
- JDBC WHERE子句条件实例
在本教程将演示如何在JDBC应用程序中,从数据库表中查询数据记录, 在查询选择记录时使用WHERE子句添加其他条件. 在执行以下示例之前,请确保您已经准备好以下操作: 具有数据库管理员权限,以在给定模 ...