请优化:#include<bits/stdc++.h> using namespace std; int t; long long a; int popcount(long long a){ long long count=0; while(a>=1){ if(a%2==1)count++; a/=2; } return count; } int main(){ cin>>t; while(t--){ cin>>a; if(popcount(a)>=3)cout<<"No,Commander"<<endl; else{ a++; if(popcount(a)<3)cout<<a<<endl; else{ while(popcount(a)>=3)a++; cout<<a<<endl; } } } return 0; }
时间: 2024-04-09 09:32:12 浏览: 71
南海区小学甲组比赛第六题
#include <iostream>
using namespace std;
int countBits(long long a) {
int count = 0;
while (a > 0) {
if (a & 1) {
count++;
}
a >>= 1;
}
return count;
}
int main() {
int t;
cin >> t;
while (t--) {
long long a;
cin >> a;
if (countBits(a) >= 3) {
cout << "No,Commander" << endl;
} else {
a++;
if (countBits(a) < 3) {
cout << a << endl;
} else {
while (countBits(a) >= 3) {
a++;
}
cout << a << endl;
}
}
}
return 0;
}
阅读全文