J - 1000>0111(此标题可能已经丢失)

题目描述:

Input
包含至多10001组测试数据。
第一行有一个正整数,表示数据的组数。
接下来每一行表示一组数据,包含两个整数l,r。
保证 0 <= l <= r <= 10^18。
Output
对于每组数据输出一行,表示最大的位或。
Sample Input
5
1 10
0 1
1023 1024
233 322
1000000000000000000 1000000000000000000
Sample Output
15
1
2047
511
1000000000000000000

分析:

本题是一个贪心问题,在一开始的时候,我一直想利用按位操作来得出正解,但是很明显没有。
参考博客:https://blog.csdn.net/qq_40984919/article/details/81367286

#include"stdio.h"
#include"string.h"
#include"math.h"
void Get_num(long long l,long long r)
{
    long long numl[65],cnt1=0;
    long long numr[65],cnt2=0;
    long long sum,ans,i;
    memset(numl,-1,sizeof(numl));
    memset(numr,-1,sizeof(numr));
    while(r)
    {
        numr[cnt2++]=r%2;
        r=r/2;
    }
    while(l)
    {
        numl[cnt1++]=l%2;
        l=l/2;
    }
    if(cnt1<cnt2)
        printf("%lld\n",(long long)pow((double)2,(double)cnt2)-1);
    else
    {
        for(--cnt2;cnt2>=0;cnt2--)
            if(numl[cnt2]!=numr[cnt2])
               break;
        ans=cnt2;
        if(ans==-1)
            ans=0;
        for(--cnt1,sum=0;cnt1>=0;cnt1--)
            if(cnt1>=ans)
             {
                 if(numr[cnt1]==1)
                 sum+=(long long)pow((double)2,(double)cnt1);
             }
             else
             {
                 sum+=(long long)pow((double)2,(double)cnt1);
             }
        printf("%lld\n",sum);
    }
}
int main()
{
    long long  T;
    long long l,r;
    while(~scanf("%lld",&T))
    {
        while(T--)
        {
            scanf("%lld%lld",&l,&r);
            if(l==r)
                printf("%lld\n",l);
            else
                Get_num(l,r);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值