题目描述:点击打开链接
这道题目还是一样的方法。使用分布的思想方法就可以了。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void solve();
int main()
{
int t,k;
scanf("%d\n",&t);
for(k=0;k < t;k++){
printf("case #%d:\n",k);
solve();
}
return 0;
}
// 1. 读入数据,直接保存在unsigned long long n里面。
// 2. 用位操作得到2进制表达式,保存在数组里面,并且记录这个数组的长度在bin_count里面。(因为不能考虑前置的零)
// 3. 设计算法从数组里面计算最长的不相同数字串的长度。
void solve(){
long long int n;
//unsigned long long i=1;
int bin[32];
int bin_count = 0;
int countLength;
int tmpLength;
int k;
scanf("%lld",&n);
while(n>0){
if(n & 1) bin[bin_count++] = 1;
else bin[bin_count++] = 0;
n >>= 1;
}
countLength = 1;
tmpLength = 1;
for(k = 1; k < bin_count ; k++){
if(bin[k] == bin[k-1]) { //这个时候发现应该停止一下
if(tmpLength > countLength){
countLength = tmpLength;
}
tmpLength = 1;
} else {
tmpLength ++;
}
}
if(tmpLength > countLength) countLength = tmpLength;
printf("%d\n",countLength);
}