码迷,mamicode.com
首页 > Windows程序 > 详细

AcWing 801. 二进制中1的个数

时间:2019-05-18 18:27:17      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:names   include   判断   http   ide   长度   ons   链接   alt   

网址 https://www.acwing.com/solution/AcWing/content/2066/

题目描述
给定一个长度为n的数列,请你求出数列中每个数的二进制表示中1的个数。

算法1
主要是使用位移和 按位与判断最后一位是否是1

int checkNum;
(checkNum &1)
checkNum >>= 1;

C++ 代码

技术图片
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int T;
 6 const int N= 1e6+100;
 7 int arr[N];
 8 
 9 void check(int checkNum)
10 {
11     int count = 0;
12 
13     while(checkNum !=0){
14         if(checkNum &1)
15             count++;
16         checkNum >>= 1;
17     }
18 
19     cout << count << " ";
20 }
21 
22 int main()
23 {
24     cin >> T;
25 
26     int idx = 0; 
27     while(T--){
28         cin >> arr[idx++];        
29     }
30 
31     for(int i =0;i < idx;i++)
32         check(arr[i]);
33 
34     return 0;
35 }
36 
37 
38 作者:defddr
39 链接:https://www.acwing.com/solution/AcWing/content/2066/
40 来源:AcWing
41 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
View Code

 

AcWing 801. 二进制中1的个数

标签:names   include   判断   http   ide   长度   ons   链接   alt   

原文地址:https://www.cnblogs.com/itdef/p/10886609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!