HDU 5272

来源:互联网 发布:mac怎么打出emoji 编辑:程序博客网 时间:2024/06/03 21:45

Dylans loves numbers

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


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
15
 

Sample Output
2

 

题意:找出一个数的二进制表示中有几个连续的1的数字段。

分析:很简单的题,用1,2,4,8.。。。。。。和给定数字&一下找出给定数字的二进制表达,然后遍历一下就可以了。


代码:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;int w[100];long long p(long long x){return 1 << x;}int main(){int t;cin >> t;while (t--){long long n;cin >> n;long long result = 0;int flag = 0;while(n){if (n&1){if (flag == 0)result++;flag = 1;}elseflag = 0;n /= 2;}cout << result << endl;}return 0;}

0 0