时空限制

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的更多相关文章

  1. HDU-1390 Binary Numbers

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

  2. Binary Numbers(HDU1390)

    Binary Numbers 点我 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum

    E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...

  4. zoj 1383 Binary Numbers

    Binary Numbers Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a positive integer n, print o ...

  5. 【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 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. 【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 ...

  10. 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

随机推荐

  1. 【转载】解决Windows和Ubuntu时间不一致的问题

    大家在切换操作系统的时候会发现一个问题, Windows 和Ubuntu的时间会出现不一致的情况.在 Windows 中把时间设置正确了过后,回到在 Ubuntu 后系统的时间又不一样了,在 Ubun ...

  2. MVC教程五:Action方法的返回类型

    MVC中的Action方法的返回值一般有以下几种: 类型 s说明 EmptyResult 不进行任何操作 ContentResult 将指定内容作为文本输出 JsonResult 输出JSON字符串 ...

  3. WebRTC 源码分析(三):安卓视频硬编码

    数据怎么送进编码器? 怎么从编码器取数据? 如何做流控? 在开始之前,我们先了解一下 MediaCodec 的基本知识. MediaCodec 基础 Developer 官网 上的描述已经很清楚了,下 ...

  4. win7 64位操作系统中 Oracle 11g 安装教程(图解)

    1.下载Oracle 11g R2 for Windows版本,下载地址如下 官方网站: http://download.oracle.com/otn/nt/oracle11g/112010/win3 ...

  5. Java设计模式(11)外观模式(Facade模式)

    外观模式(Facade)的定义:为子系统中的一组接口提供一个一致的界面. Facade一个典型应用就是数据库JDBC的应用,如下例对数据库的操作: public class DBCompare { C ...

  6. android下通过app名字打开程序(activity)链接

    Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:00000099 EndFragment:00004599 1.手机遥控器模拟快捷键启动app 刚开 ...

  7. App Store那些事儿

    5条建议优化手机游戏的苹果App Store截图 第一:遵守应用商店的规定 进入应用商店的每一款应用都要加上至少一张截图,并且尺寸大小必须符合应用商店的惯例. 第二:显示营销信息 既然你已经知道规定的 ...

  8. MFC绘图小实验(2)

    1,以正五边形的5个顶点为基础,隔点存储构成五角星.填充模式采用WINDING.五角星边界线为5个像素宽的蓝色实线,内部使用红色填充. CRect rect; //定义矩形 GetClientRect ...

  9. [转]jQuery选择器 (详解)

    1).基本 #id 根据给定的ID匹配一个元素.例如:$("#id")element 根据给定的元素名匹配所有元素.例如:$("div").class 根据给定 ...

  10. e838. 使JTabbedPane中的卡片能用按键的方式选取

    Setting a mnemonic on a tab allows the tab to be selected with a keystroke. For example, if the mnem ...