✅ LV. 2
1 로 변환될 때마다 count해주기n 다음 수 중 1의 개수가 n 과 동일할 경우 returnusing namespace std;
int cnt;
void f(int n) {
if(n>1) f(n/2);
if(n%2==1) cnt++;
}
int solution(int n) {
int answer = 0;
cnt=0;
f(n);
int c=cnt;
while(1) {
cnt=0;
n++;
f(n);
if(cnt==c) return n;
}
return answer;
}
bitset 함수 사용해 변환하기n 을 이진변환해 count 한 1 의 개수를 저장하고 저장한 개수와 n 다음의 수들의 1 의 개수가 동일할 때까지 반복하기#include <bitset>
using namespace std;
int solution(int n) {
int num = bitset<20>(n).count();
while (bitset<20>(++n).count() != num);
return n;
}