문제

https://school.programmers.co.kr/learn/courses/30/lessons/12911

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

내가 쓴 코드

#include <string>
#include <vector>

using namespace std;

int binary_one_count(int n)
{
    int cnt = 0;
    while (true)
    {
        if (n % 2 == 1) cnt++;
        n /= 2;
        if (n / 2 < 1)
        {
            cnt++;
            break;
        }
    }
    return cnt;
}

int solution(int n) {
    int answer = n;
    int n_cnt = binary_one_count(n);

    while (n_cnt != binary_one_count(++answer));

    return answer;
}

 

다른 사람 코드

비트셋 머지...?

#include <bitset>

using namespace std;

int solution(int n) {
    int num = bitset<20>(n).count();

    while (bitset<20>(++n).count() != num);
    return n;
}
Posted by pi92

블로그 이미지
pi92

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.7
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

최근에 올라온 글

최근에 달린 댓글

글 보관함