题目
解题思路
把数据mod2之后储存在数据b和前一个mod2的数据a进行对比,然后对本身的数据/=2,然后用count来计数即可,一旦遇到相同的二进制串就重新初始化即可
代码
#include<iostream>
using namespace std;
int max_len_dif(long long a)
{
int fir = a % 2,sec;
int count = 1;
int temp = 1;
while(a)
{
a >>= 1;
sec = a % 2;
if(sec != fir)
{temp++;}
else
{
if(temp > count)
{count = temp;}
temp = 1;
}
fir = sec;
}
if(temp > count){count = --temp;}
//需要注意的是,如果在while循环没有重新初始化,需要在while循环外面再单独处理
return count;
}
int main()
{
int total;
cin >> total;
for(int i = 0; i < total; i++)
{
long long a;
cin >> a;
cout << "case #" << i << ":"<<endl;
cout << max_len_dif(a) << endl;
}
return 0;
}