-
[프로그래머스/Level 2] 다음 큰 숫자 (C++)알고리즘 문제풀이/프로그래머스 2020. 11. 6. 16:13
programmers.co.kr/learn/courses/30/lessons/12911
코딩테스트 연습 - 다음 큰 숫자
자연수 n이 주어졌을 때, n의 다음 큰 숫자는 다음과 같이 정의 합니다. 조건 1. n의 다음 큰 숫자는 n보다 큰 자연수 입니다. 조건 2. n의 다음 큰 숫자와 n은 2진수로 변환했을 때 1의 갯수가 같습니
programmers.co.kr
1. n을 2진수로 변경한 후, 1의 개수를 구하는 getCount() 함수 구현
2. 입력받은 n의 getCount()에 넣어 개수를 저장
3. n을 1씩 증가시키면서 1의 개수가 같은 경우를 찾으면 종료하고 n을 리턴#include <string> #include <vector> #include <algorithm> using namespace std; int getCount(int n) { int cnt = 0; while(n != 0) { if(n % 2 == 1) cnt++; n /= 2; } return cnt; } int solution(int n) { int cnt = getCount(n); while(1) { n++; if(getCount(n) == cnt) break; } return n; }'알고리즘 문제풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스/Level 2] 땅따먹기 (C++) (0) 2020.11.06 [프로그래머스/Level 2] 올바른 괄호 (C++) (0) 2020.11.06 [프로그래머스/Level 2] 가장 큰 정사각형 찾기 (C++) (0) 2020.11.05 [프로그래머스/Level 2] 카펫 (C++) (0) 2020.11.05 [프로그래머스/Level 2] 타겟 넘버 (C++) (0) 2020.11.05