알고리즘/프로그래머스

[프로그래머스] 12911. 다음 큰 숫자

Master potato 2024. 4. 13. 22:40
12911 문제.

 

 

해답
def solution(n):
    answer = 0
    next_n = n
    # 자연수 n의 이진수 1의 개수
    n_one_count = format(n, 'b').count('1')
    
    # 제한사항 n은 1,000,000 이하
    while next_n < 1_000_000:
        next_n += 1
        # n의 다음 자연수의 이진수 1의 개수
        next_n_one_count = format(next_n, 'b').count('1')
        if n_one_count == next_n_one_count:
            answer = next_n
            break
    return answer

 

출처:

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

 

프로그래머스

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

programmers.co.kr