HDU-5272

Dylans loves numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 201    Accepted Submission(s): 136


Problem Description
Who is Dylans?You can find his ID in UOJ and Codeforces.
His another ID is s1451900 in BestCoder.

And now today's problems are all about him.

Dylans is given a number N.
He wants to find out how many groups of "1" in its Binary representation.

If there are some "0"(at least one)that are between two "1",
then we call these two "1" are not in a group,otherwise they are in a group.
 
Input
In the first line there is a number T.

T is the test number.

In the next T lines there is a number N.

0N1018,T1000
 
Output
For each test case,output an answer.
 
Sample Input
1
5
 
Sample Output
2
bestcoder round#45 1001
/**
          题意:给出一个数,然后求该数的二进制的有多少组1 
                    比如10101     1101101   都是三组 
          做法:暴力 还有如果计算一个数的二进制有多少1
          可以
          while(n)
          {
                    count++;
                    n = n&(n-1);
          }
**/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
//#ifndef ONLINE_JUDGE
//          freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
          int T;
          scanf("%d",&T);
          while(T--)
          {
                    long long n;
                    long long sum = 0;
                    int tt = 0;
                    scanf("%lld",&n);
                    int cnt = 0;
                    while(n)
                    {
                              cnt = n % 2;
                              if(tt == 0 && cnt == 1) sum++;
                              n /= 2;
                              tt = cnt;
                    }
                    printf("%lld
",sum);
          }
          return 0;
}
原文地址:https://www.cnblogs.com/chenyang920/p/4592465.html