zoukankan      html  css  js  c++  java
  • hdu 5272 Dylans loves numbers 水题

    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=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //**************************************************************************************
    
    int main()
    {
        int t=read();
        while(t--){
            ll n=read();
            int ans=0;
            int flag=1;
            while(n)
            {
                if(flag)
                {
                    if(n&1)
                    {
                        ans++;
                        flag=0;
                    }
                }
                else if(!(n&1))
                {
                    flag=1;
                }
                n>>=1;
            }
            cout<<ans<<endl;
        }
    }
  • 相关阅读:
    Uva 10935 Throwing cards away I
    Uva 3226 Symmetry
    eclipse @ 注释为何一写就报错
    2015省赛小感想
    Zoj 3842 Beauty of Array
    fedora 设置命令别名
    Uva220 Othello
    工作小技巧积累
    SSL介绍与Java实例
    一个完整的SSL连接建立过程
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4591031.html
Copyright © 2011-2022 走看看