How many '1's are there题解
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题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
- JSOI2016R3 瞎BB题解
题意请看absi大爷的blog http://absi2011.is-programmer.com/posts/200920.html http://absi2011.is-programmer.co ...
随机推荐
- 使用基于Caffe的MobileNet分类踩坑备忘录
首先要帮Caffe甩个锅:Caffe对图像处理进行了很高明的封装,以protobuffer形式组织的搭积木式的网络构建也很灵活方便,这里的坑都是自己腿不好,走路不稳崴进去的. 1. Caffe的一个i ...
- libvirt kvm云主机监控
libvirt
- dumpbin检查Dll
用dumpbin.exe工具查看DLL,dumpbin.exe是VS自带的工具.版本VS2013,路径是:G:\VS2013\VC\bin\amd64\ 可以看到dumpbin.exe. 使用VS里的 ...
- nohup不输出日志信息的方法及linux重定向
最近使用nohup创建了一个后台进程,默认日志输出到了nohup.out文件中,程序跑起来也就没再管,过了大约一周,发现硬盘空间不够了,于是查找原因,发现这个nohup.out文件已经到了70G了,导 ...
- Flutter实战视频-移动电商-21.分类页_类别信息接口调试
21.分类页_类别信息接口调试 先解决一个坑 取消上面的GridVIew的回弹效果.就是在拖这个gridview的时候有一个滚动的效果 physics: NeverScrollableScrollPh ...
- 09.客户端集成IdentityServer
09.客户端集成IdentityServer 新建API的项目 dotnet new webapi --name ClientCredentialApi 在我们上一节课的代码IdentityServe ...
- 01 mybatis框架整体概况(2018.7.10)-
01 mybatis框架整体概况(2018.7.10)- F:\廖雪峰 JavaEE 企业级分布式高级架构师课程\廖雪峰JavaEE一期\第一课(2018.7.10) maven用的是3.39的版本 ...
- Eclipse如何查看接口实现类快捷键
1.找到要打开的接口类 2.双击接口名选中 3.按Ctrl+T打开接口实现类 以List接口为例,如下所示
- 怎样使一个INPUT框里的文字在框被点击后自动全选或清除?
$("#smsContent").focus(function(){ this.select(); }); <input name="keywords" ...
- E20180712-hm
directive n. 指令; <美>命令,训令,指令; 方针;