文章标题 HDU 5272:Dylans loves numbers( 水)
来源:互联网 发布:大数据新闻是什么 编辑:程序博客网 时间:2024/05/29 17:32
Dylans loves numbers
Description
Who is Dylans?You can find his ID in UOJ and Codeforces.
His another ID is s1451900 in BestCoder.
And now today’s problems are all about him.
Dylans is given a number
He wants to find out how many groups of “1” in its Binary representation.
If there are some “0”(at least one)that are between two “1”,
then we call these two “1” are not in a group,otherwise they are in a group.
Input
In the first line there is a number
In the next
Output
For each test case,output an answer.
Sample Input
1
5
Sample Output
2
题意:给你一个数,然后叫你求这个数以二进制表示时有多少组1,当两个1之间有至少一个0,这两个1是不同的两个组。比如5,其二进制数为101,中间有一个0,所以最终有两组1。
分析:首先可以将所给 的数的二进制存进一个数组,然后判断这个数组有多少组1就行了。
代码:
#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<vector>#include<math.h>#include<queue> #include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;long long n;int main (){ int t; scanf ("%d",&t); long long cnt; int a[70]; while (t--){ cnt=0; scanf ("%lld",&n); int k=0; int temp; while (n){//将n的二进制数存进a数组 temp=n%2; a[k++]=temp; n/=2; } if (a[0]==1) cnt++; for (int i=1;i<k;i++){//判断有多少组1 if (a[i]==1&&a[i]!=a[i-1]){//当当前数为1且前一个数为0,则加1; cnt++; } } if (n==1||n==2||n==3||n==4)printf ("1\n"); printf ("%lld\n",cnt); } return 0;}
- 文章标题 HDU 5272:Dylans loves numbers( 水)
- HDU 5272 Dylans loves numbers
- HDU 5272Dylans loves numbers
- HDU 5272 Dylans loves numbers
- hdu 5272 Dylans loves numbers
- HDU 5272 Dylans loves numbers
- HDU 5272 : Dylans loves numbers
- HDU 5272 Dylans loves numbers
- HDU - 5272 Dylans loves numbers
- HDU 5272 Dylans loves numbers 二进制
- Dylans loves numbers
- HDU 5272 Dylans loves numbers——BestCoder Round #45(模拟)
- HDU 5274 Dylans loves tree 树链剖分(水)
- hdu 5273 Dylans loves sequence
- HDU 5273Dylans loves sequence
- hdu 5273 Dylans loves sequence
- HDU 5273 Dylans loves sequence
- HDU 5273 Dylans loves sequence
- 教你步步为营掌握自定义 View
- axis2生成的client在weblogic下调不通的问题
- [leetcode]148. Sort List -- JavaScript代码
- 解决Linux环境下Tomcat日志乱码的问题
- 汇编指令英文全称
- 文章标题 HDU 5272:Dylans loves numbers( 水)
- Bitmap类getPixels()详解
- BZOJ2530 [Poi2011]Party
- 深刻理解Python中的元类(metaclass)
- java 实现https
- java代码优化
- 认清Android框架 MVC,MVP和MVVM
- ios 跳转指定页面和插入页面
- SVN for exclipse