Description:

Description:

第一行输入数字n(n<=50),表示有n组测试用例,第2到第n+1行每行输入数m(m为整数),统计并输出m用二进制表示时,1的个数。

例如:m=9时,二进制表示为1001,则输出2.

Input:

2

3

7

Output:

2

3


Hint:

利用位运算


一道比较简单的题目,其中也可以有高深的做法。虽然提示位运算,但因为不熟悉所以并没有用位运算。
 

有错误的代码:(被电脑管家直接视为间谍软件给kill掉了。。)不太懂错的地方,先贴出来吧
#include <stdio.h>
int main() {
int binary[];
int count = ;
int one = ;
int n, i, num;
scanf("%d", &n);
for (i = ; i < n; i++) {
count = ;
one = ;
scanf("%d", &num);
while (num != ) {
binary[count] = num % ;
num /= ;
count++;
}
for (i = count-; i >= ; i--) {
if (binary[i] == )
one++;
}
printf("%d\n", one);
}
}

最后通过的代码:

#include <stdio.h>
int main() {
int binary[];
int n, i, num, j;
scanf("%d", &n);
for (i = ; i < n; i++) {
int count = ;
int one = ;
scanf("%d", &num);
while (num != ) {
binary[count] = num % ;
if (binary[count] == ) {
one++;
}
num /= ;
count++;
}
printf("%d\n", one);
}
}

标答:

#include<stdio.h>

int bitcount(int x) {
int count = ;
while (x != ) {
x &= (x-);
count++;
}
return count;
} int main() {
int num;
int x;
scanf("%d", &num); while (num--) {
scanf("%d", &x);
printf("%d\n", bitcount(x));
}
return ;
}

听说有一个函数

How many '1's are there题解的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

  10. JSOI2016R3 瞎BB题解

    题意请看absi大爷的blog http://absi2011.is-programmer.com/posts/200920.html http://absi2011.is-programmer.co ...

随机推荐

  1. 2.CSS 颜色代码大全

    确实使用,不用重复造轮子了!!! 摘自:http://www.cnblogs.com/circlebreak/p/6140602.html

  2. URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)

    URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) 有时候在IDEA中配置spring文件 ...

  3. CF-807C

    C. Success Rate time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. A - Alyona and Numbers

    Description After finishing eating her bun, Alyona came up with two integers n and m. She decided to ...

  5. 极客时间_Vue开发实战_04.开发环境搭建

    Vue CLI的形式搭建环境: vue create hello-world 我们选择default默认的配置,提供babel和eslint的支持.如果你已经对工程化的东西非常了解了.你可以选择自定义 ...

  6. C#项目中一些文件类型说明

    designer.cs  是窗体设计器生成的代码文件,作用是对窗体上的控件做初始化工作 (在函数InitializeComponent()中)VS2003以前都把这部分代码放到窗体的cs文件中,由于这 ...

  7. selenium浏览器驱动下载地址整理

    今天把手头有的一些关于selenium测试的资源整理了一下,分享出来. 1. 所有版本chrome下载 是不是很难找到老版本的chrome?博主收集了几个下载chrome老版本的网站,其中哪个下载的是 ...

  8. 1107 Social Clusters (30 分)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  9. html实现点击图片放大功能

    话不多说,直接上代码 <html> <head> <style> .over {position: fixed; left:0; top:0; width:100% ...

  10. Multi-University板块

    力争补完所有 Multi-University 的"水题",任重而道远. HDU2819[二分匹配与矩阵性质] HDU2844[背包问题(二进制优化)] HDU2824[欧拉函数] ...