HDU1390_Binary Numbers【水题】【位运算】

Binary Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2828    Accepted Submission(s): 1755

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

Source

Central Europe 2001, Practice

题目大意:给你一个数N,输出N的二进制形式上为1的数位(从右至左)

思路:每次(N&1)判断末尾是否为1,为1则存入数组ans[]中,不为1则

不存,之后数位自增,N向右移一位,继续判断末尾……

#include<stdio.h>
#include<string.h>
int ans[110];
int main()
{
    int N,T;
    scanf("%d",&T);
    while(T--)
    {
        memset(ans,0,sizeof(ans));
        scanf("%d",&N);
        int num = 0,count = 0;
        while(N)
        {
            if(N&1)
            {
                ans[num++] = count;
                count++;
            }
            else
            {
                count++;
            }
            N >>= 1;
        }
        for(int i = 0; i < num; i++)
        {
            if(i!=num-1)
                printf("%d ",ans[i]);
            else
                printf("%d\n",ans[i]);
        }
    }

    return 0;
}
时间: 2024-12-15 06:50:51

HDU1390_Binary Numbers【水题】【位运算】的相关文章

bzoj5108 [CodePlus2017]可做题 位运算dp+离散

[CodePlus2017]可做题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 87  Solved: 63[Submit][Status][Discuss] Description qmqmqm希望给sublinekelzrip出一道可做题.于是他想到了这么一道题目:给一个长度为n的非负整数序列ai,你需 要计算其异或前缀和bi,满足条件b1=a1,bi=bi?1 xor ai(i≥2).但是由于数据生成器出现了问题,他生成的序列a 的长度特

[leetcode] Bitwise AND of Numbers Range(位运算)

不得不说位运算的应用都是很巧妙的. 这道题求一个连续区间整数的与运算的值,突破点在于连续的区间这个特点.我们可以先找几个数看一下规律, 2 3 4 5的二进制分别是 10 ,11,100,101,可以发现若m==n,则m为答案:当m!=n时,因为连续的两个数的二进制 的最后一位肯定不一样,与的值一定是0,前面相同的部分(1&1=1,0&0=0)保持. 所以我们每次先判断,不同的话就右移一位,比较前面的,直到相同的时候结束,最后左移  移动的位数. 不必担心左移的时候有1会导致溢出,从而失败

POJ 2739 Sum of Consecutive Prime Numbers(水题)

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20560   Accepted: 11243 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

hdu 5272 Dylans loves numbers 水题

Dylans loves numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5272 Description Dylans是谁?你可以在 UOJ 和 Codeforces上看到他.在BestCoder里,他有另外一个ID:s1451900.今天的题目都和他有关哦.Dylans得到了一个数N.他想知道N的二进制中有几组1.如果两个1之间有若干个(至少一个)0

hdu 5108 Alexandra and Prime Numbers (水题,暴力)

Problem Description Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given an positive integer N, judge whether N is prime. The problem above is quite easy, so Alexandra gave him a new task: Given

CodeForces 682A Alyona and Numbers (水题,数学)

题意:给定两个数 m,n,问你在从1到 n,和从 1到 m中任选两个数加起来是5的倍数,问你有多少个. 析:先计算 m 和 n中有多少个取模5是从0到4的,然后根据排列组合,相乘就得到了小于等于 m 和 n的并且能整除五的个数,然后再加上剩下的. 代码如下: #include <bits/stdc++.h> using namespace std; typedef long long LL; const int aa = 1234567; const int bb = 123456; cons

[位运算] [搜索] [递推优化] [计算几何] TEST 2016.7.15

NOIP2014 提高组模拟试题 第一试试题 题目概况: 中文题目名称 合理种植 排队 科技节 源程序文件名 plant.pas/.c/.cpp lineup.pas/.c/.cpp scifest.pas/.c/.cpp 输入文件名 plant.in lineup.in scifest.in 输出文件名 plant.out lineup.out scifest.out 每个测试点时限 1s 1s 1s 测试点数目 10 10 10 每个测试点分值 10 10 10 内存上限 128MB 128

关于位运算的水题

找数字2 Time Limit: 25000ms, Special Time Limit:50000ms, Memory Limit:32768KB Total submit users: 92, Accepted users: 67 Problem 11466 : No special judgement Problem description 给定2n+1个数字,只有一个数字出现了奇数次,其余的数字都出现了偶数次,现在你需要找出出现奇数次的数字. Input 包含多组数据,每组数据第一行为一

HDU1196_Lowest Bit【位运算】【水题】

Lowest Bit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8043    Accepted Submission(s): 5920 Problem Description Given an positive integer A (1 <= A <= 100), output the lowest bit of A. For

Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 题意: