본문 바로가기

개발/알고리즘

프로그래머스 다음 큰 숫자 C++

문제

 

풀이

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int countOne(int n)
{   
    vector<int> v;
    while(n > 1)
    {
        v.push_back(n % 2);
        n /= 2;
    }

    if( n == 1)
    {
        v.push_back(1);
    }
    
    return count(v.begin(), v.end(), 1);
}

int solution(int n) {
    int cnt = countOne(n);
    int answer(0);
    n++;
    while(1)
    {
        if(cnt == countOne(n))
        {
            answer = n;
            break;
        }
        else
            n++;
    }
      
    return answer;   
}

 

결과

 

걸린시간

30분

'개발 > 알고리즘' 카테고리의 다른 글

백준 2309 일곱 난쟁이 C++  (0) 2021.01.03
조합 알고리즘 C++  (0) 2021.01.02
프로그래머스 JadenCase 문자열 만들기 C++  (0) 2020.12.23
프로그래머스 튜플 C++  (0) 2020.12.22
프로그래머스 피보나치 수 C++  (0) 2020.12.15