标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 201 Accepted Submission(s): 136
/** 题意:给出一个数,然后求该数的二进制的有多少组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\n",sum); } return 0; }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4592465.html