• hdu 5491(位运算)


    题意:给你n,a,b. 希望得到比n大,二进制1的个数在 a ,b之间的最小的数

    思路:①满足条件,输出

               ②num < a 从右找到是0的最小位,变成1

               ③num > b从右到左找是1的最小位,加上一,即 n + 2 ^ i


    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <time.h>
    typedef long long ll;
    using namespace std;
    int d[35];
    
    int get(ll x)
    {
        int all = 0,t = 0;
        while(x)
        {
            if(x & 1)
            {
                all ++;
                d[t++] = 1;
            }
            else
                d[t++] = 0;
            x >>= 1;
        }
        return all;
    }
    
    ll Pow(int x,int n)
    {
        ll ans=1;
        while(n)
        {
            if(n%2)ans=ans*x;
            x=x*x;
            n=n/2;
        }
        return ans;
    }
    
    int main()
    {
        int a,b,t,num;
        ll n;
        scanf("%d", &t);
        int Cas = 1;
        while(t--)
        {
            scanf("%I64d%d%d",&n,&a,&b);
            memset(d,0,sizeof(d));
            n++;
            num = get(n);
            while(1)
            {
                if(num >= a && num <= b)
                {
                    printf("Case #%d: %I64d
    ",Cas++,n);
                    break;
                }
                else if(num < a)
                {
                    int t = 0;
                    while(d[t])
                        t++;
                    d[t] = 1;
                    n += Pow(2,t);
                    num++;
                }
                else if(num > b)
                {
                    int t = 0;
                    while(!d[t])
                        t++;
                    n += Pow(2,t);
                    num = get(n);
                }
            }
        }
        return 0;
    }
    

      


    ps.我们需要的就是不停找借口让自己坚持下去


  • 相关阅读:
    Centos7 ifconfig命令找不到
    request的各种方法
    linux开放端口
    easyui datagrid 部分参数
    设置tomcat内存
    tomcat做成系统服务
    Meta-analysis with complex research designs: dealing with dependence from multiple measures and multiple group comparisons
    多重校正
    DTI
    learning source archive
  • 原文地址:https://www.cnblogs.com/Przz/p/5409743.html
Copyright © 2020-2023  润新知