알고리즘/프로그래머스

Programers 다음 큰 숫자

내이름은 킹햄찌 2022. 7. 26. 23:04

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

 

프로그래머스

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

programmers.co.kr

 

프로그래머스 다음 큰 숫자 문제입니다.

 

아이디어

Greedy문제입니다. 비트에 대한 이해도를 판단 할 수 있는 문제로 느껴집니다.

아래의 풀이가 어렵다면 비트 관련 문제를 풀어보시는 것을 추천드립니다.

 

#include <string>
#include <vector>
using namespace std;

//bit 개수 return
int getBitCount(int n) {
	int bitCnt = 0;
	while (n) {
		if (n % 2)
			bitCnt++;
		n /= 2;
	}
	return bitCnt;
}

int solution(int n) {
	int bitCnt = getBitCount(n);
	//비트 개수가 같은 수를 찾을때까지 반복
	while (1) {
		n++;
		if (bitCnt == getBitCount(n))
			break;
	}
	int answer = n;
	return answer;
}