算法
- 方法2我用的『前缀和』
方法1、参考牛油bitset
参考其他牛油
while (cin >> n)
{
int ans=0,cnt=0;
bitset<32> bs(n);//学习这里 方法2、我模拟的bitset
#include<bits/stdc++.h>
using namespace std;
int PreSum[8];
void solve( int n )
{
int test=0x0001;
int loop=8;
while( loop-- )
{
if( 7==loop && (n&test) )
{
PreSum[loop]=1;
test<<=1;
continue;
}
if( (n&test) && PreSum[loop+1] )
{
PreSum[loop]=PreSum[loop+1]+1;
test<<=1;
}
else if( n&test )
{
PreSum[loop]=1;
test<<=1;
}
else
{
PreSum[loop]=0;
test<<=1;
}
}
}
int main()
{
int n;
while( ~scanf("%d",&n) )
{
memset( PreSum, 0, sizeof( PreSum) );
solve(n);
int out=0;
for( auto num : PreSum )
{
out=max( out, num );
}
printf("%d\n", out );
}
return 0;
} 
京公网安备 11010502036488号