天天看點

寒假集訓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;
}