Dylans loves numbers

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5272

Description

Dylans是谁?你可以在 UOJ 和 Codeforces上看到他。
在BestCoder里,他有另外一个ID:s1451900。
今天的题目都和他有关哦。
Dylans得到了一个数N。他想知道N的二进制中有几组1。
如果两个1之间有若干个(至少一个)0 “挡住”,他们就不是同一组的,
否则他们就是同一组的。

Input

第一行读入一个数T表示数据组数。
接下来T行,每行一个数N。
0≤N≤1018,T≤1000

Output

对于每组数据,输出一个数表示答案。

Sample Input

1
5

Sample Output

2

HINT

题意

题解:

这道题就是按照题意模拟。
设读入的数是N,我们先把N分解成二进制形式放在数组A里。(正反顺序没有关系)
然后对A数组循环一边,如果当前位置是1而前一位是0那么计数器就++。注意一些小的细节。
时间复杂度为O(T∗log(N))

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-5
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int main()
{
int t=read();
while(t--){
ll n=read();
int ans=;
int flag=;
while(n)
{
if(flag)
{
if(n&)
{
ans++;
flag=;
}
}
else if(!(n&))
{
flag=;
}
n>>=;
}
cout<<ans<<endl;
}
}

最新文章

  1. jquery实现表格的搜索功能
  2. 关于C#垃圾回收
  3. linux 挂载光盘:mount: you must specify the filesystem type
  4. JS&amp;CSS文件请求合并及压缩处理研究(五)
  5. Tyk API网关介绍及安装说明
  6. 反射工具类——ReflectUtils
  7. MVC ActionResult视图结果
  8. 【python】 web开发入门
  9. LC-检索
  10. bzoj2004
  11. UVA 327 -Evaluating Simple C Expressions(栈)
  12. IOS深入学习(1)之图标文件(icon files)
  13. PostOffice最小距离问题
  14. syslog的坑
  15. python 计算程序运行耗时的好用的代码
  16. Django form choices, placeholder
  17. linux nfs服务配置挂载以及oracle使用nfs存储挂载注意事项
  18. Javascript数据类型转换规则
  19. echo 与 od -x 与 %!xxd 命令
  20. 《OpenGL编程指南第七版》学习——编译时提示“error C2381: “exit” : 重定义;__declspec(noreturn) 不同”错误的解决办法

热门文章

  1. mysql Access denied for user \&#39;root\&#39;@\&#39;localhost\&#39;”解决办法总结,下面我们对常见的出现的一些错误代码进行分析并给出解决办法,有需要的朋友可参考一下。
  2. Linux下安装Vapor
  3. PHP 代码质量检测工具的安装与使用
  4. 捣蛋phpwind过滤器执行流程
  5. 耳机jack构造及在应用时可能出现的问题
  6. 获取子窗口中使用jQuery.data()设置的参数
  7. css 简单的 before after 笔记
  8. C#UDP(接收和发送源码)源码完整
  9. hdu 1045 Fire Net(最小覆盖点+构图(缩点))
  10. JavaScript学习——内置属性