프로그래머스 - 다음 큰 숫자

well-life-gm·2021년 12월 21일
0

프로그래머스

목록 보기
104/125

프로그래머스 - 다음 큰 숫자

__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;
}

결과

profile
내가 보려고 만든 블로그

0개의 댓글