好吧理解有误
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int T, cnt;
stack<int> myStack;
int a[100];
void getBinary(ll num)
{
while(num)
{
myStack.push(num%2);
num/=2;
}
}
int main()
{
cin>>T;
while(T--){
ll num;
cin>>num;
getBinary(num);
int l=0;
while(!myStack.empty()){
a[l++]=myStack.top();
myStack.pop();
}
int len=1;
int maxi=len;
for(int i=1;i<l;++i){
if(a[i]!=a[i-1]) len++;
else {
if(a[i]==0) {
len=0;
continue;
}
else len=1;
}
if(maxi<len) maxi=len;//after every loop, if true, we update maxi
}
printf("case #%d:\n",cnt++);
cout<<maxi<<endl;
}
return 0;
}
这个过了
using namespace std;
int main() {
int t, count = 0;
cin >> t;
while (t–) {
int n;
cin >> n;
int pre = n % 2;
n /= 2;
int s = 1, max = 1;
while (n) {
int temp = n % 2;
n /= 2;
if (pre == temp)
s = 1;
else if (pre != temp)
s++;
if (s > max)
max = s;
pre = temp;
}
cout << “case #” << count++ << “:\n”;
cout << max << endl;
}
return 0;
}
这个过了
using namespace std;
int main() {
int t, count = 0;
cin >> t;
while (t–) {
int n;
cin >> n;
int pre = n % 2;
n /= 2;
int s = 1, max = 1;
while (n) {
int temp = n % 2;
n /= 2;
if (pre == temp)
s = 1;
else if (pre != temp)
s++;
if (s > max)
max = s;
pre = temp;
}
cout << “case #” << count++ << “:\n”;
cout << max << endl;
}
return 0;
}
跪了,求大神提供错的测试数据
int cmp(const voida,const voidb)
{
return (int)b-(int)a;
}
int main()
{
int t,x,y,q[33],i=0,e;
scanf(“%d”,&t);
while(t–)
{
scanf(“%d”,&x);
y=0;
for(e=0;e<33;e++)
q[e]=1;
while(x/2)
{
if((x%2)!=((x/2)%2))
q[y]++;
else
y++;
x>>=1;
}
qsort(q,32,sizeof(q[0]),cmp);
printf(“case #%d:\n”,i);
printf(“%d\n”,q[0]);
}
return 0;
}
case编号没有自加,t>1的所有数据都是WA……加上以后可以ac。