__builtin_popcount 혹은 bitset을 사용하면 된다.
코드는 다음과 같다.
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
int n_count = __builtin_popcount(n);
while(1) {
n++;
if(__builtin_popcount(n) == n_count)
break;
}
return n;
}
#include <bitset>
using namespace std;
int solution(int n) {
int n_count = bitset<20>(n).count();
while(1) {
n++;
if(bitset<20>(n).count() == n_count)
break;
}
return n;
}