天天看点

寒假集训2 B hdu 5272 关于二进制

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5272

判断一个数中有多少组不连续的1,注意是组,即1101有2组,101也有两组

主要通过移位运算判断#include <iostream>

#include <iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define N 100005
#define ll long long


int main()
{
    int t;
    scanf("%d", &t);
    ll n, x, temp;
    while (t--)
    {
        int b;
        scanf("%I64d", &n);
        x = n;
        int flag = 0;
        for (b = 0; x != 0; x >>= 1)
        {
            if (x & 01)
            {
                if(!flag)
                {
                   b++;
                   flag = 1;
                }
            }
            else
            {
                flag = 0; //标记上一位为0
            }
        }
        printf("%d\n", b);
    }

    return 0;
}