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. 洛谷 1079 Vigenère 密码——模拟水题

    题目:https://www.luogu.org/problemnew/show/P1079 大水题. #include<iostream> #include<cstdio> ...

  2. poj3070 求斐波那契数列第n项 ——矩阵快速幂

    题目:http://poj.org/problem?id=3070 用矩阵快速幂加速递推. 代码如下: #include<iostream> #include<cstdio> ...

  3. atom 的一些东东

    一. 配置atom atom 有些插件被墙了, 往往导致无法下载插件, 网上查了一些解决方案, 大部分就两种解决方案. 配置国内源 离线下载插件 1. 配置国内源 Linux 在 /home/user ...

  4. Android开发--Activity

    一:Activity生命周期 (1)Activity生命周期中的几种方法: protected void onCreate(Bundle savedInstanceState): protected  ...

  5. Empire Strikes Back

    题意: 给定$K$个数字,求最小的正整数$n$,使得$\prod_{i=1}^{K}{a_i !} | n!$ 解法: 注意到$$\sum_{p为质数}{1/p} = O(loglogn)$$, 这样 ...

  6. git搭建私有仓库

    git gui参考 https://ask.helplib.com/git/post_1004941

  7. WordCount作业提交到FileInputFormat类中split切分算法和host选择算法过程源码分析

    参考 FileInputFormat类中split切分算法和host选择算法介绍  以及 Hadoop2.6.0的FileInputFormat的任务切分原理分析(即如何控制FileInputForm ...

  8. Aaronson

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  9. Flutter实战视频-移动电商-13.首页_广告Banner组件制作

    13.首页_广告Banner组件制作 主要是做这个小广告条. 其实就是读取一个图片做一个widget放到这里 使用stlessW快速生成 定义一个变量存放图片的url地址: 这样我们的广告条就写完了 ...

  10. struts2注解的作用

    Struts2注解 1 Struts2注解的作用 使用注解可以用来替换struts.xml配置文件!!! 2 导包 必须导入struts2-convention-plugin-2.3.15.jar包, ...