●문제

●입출력

문제해독 : 숫자 n 이 주어졌을때 result값은 항상 n보다 크며
10진수값인 n을 2진수값으로 변환했을때 1의 갯수가 같은 값중에 가장 작은값 도출
내가 푼 방법 : bitset을 사용하여 1의갯수를 구하고 while문으로 result값을 도출
#include <string>
#include <vector>
#include <iostream>
#include <bitset>
using namespace std;
int solution(int n)
{
//n이 주어질때 다음 temp값은 n보다 크다 temp > n
//temp값과 n값은 2진수 변환했을때 1의 갯수가 같다
//temp값 조건만족하는 가장 작은 수
//1의 갯수 카운트
int oneCount = bitset<32>(n).count();
int temp = n + 1;
while (true)
{
if (bitset<32>(temp).count() == oneCount)
{
return temp;
}
temp++;
}
return -1;
}
int main()
{
int n;
cout << "n값을 입력하세요: " << endl;
cin >> n;
int result = solution(n);
cout << "result값 : " << result << " 입니다" << endl;
return 0;
}
레벨 : 2
점수 : 1
'C++ 프로그래머스 > 기타 문제' 카테고리의 다른 글
| 프로그래머스(C++) - 귤 고르기 (0) | 2025.10.10 |
|---|---|
| 프로그래머스(C++) - 멀리뛰기 (0) | 2025.10.10 |
| 프로그래머스(C++) - 숫자의 표현 (0) | 2025.08.01 |
| 프로그래머스(C++) - JadenCase 문자열 만들기 (0) | 2025.08.01 |
| 프로그래머스(C++) - 콜라 문제 (0) | 2025.05.13 |
