codeforces C. Bits(数学题+或运算)

题意:给定一个区间,求区间中的一个数,这个数表示成二进制的时候,数字1的个数最多!
如果有多个这样的数字,输出最小的那个!

思路:对左区间的这个数lx的二进制 从右往左将0变成1,直到lx的值大于右区间的值rx!

 1 #include<iostream> 
 2 #include<cstring>
 3 #include<cstdio> 
 4 #include<algorithm>
 5 
 6 using namespace std;
 7 
 8 int main(){
 9     long long a, b;
10     int n;
11     cin>>n;
12     while(n--){
13         cin>>a>>b;
14         for(long long i=1; (a|i) <= b; i<<=1)
15             a |= i;
16         cout<<a<<endl; 
17     }
18     return 0; 
19 } 
View Code
原文地址:https://www.cnblogs.com/hujunzheng/p/4080205.html