Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- 분할정복
- 백준
- 그래프
- stoi
- int
- 우선순위큐
- 정렬
- DP
- 문자열
- 유클리드호제법
- C++
- map
- 에라토스테네스의 체
- 다이나믹프로그래밍
- 최소공배수
- vector
- 배열
- DFS
- Sort
- 티스토리챌린지
- N과M
- 백트래킹
- 프로그래머스
- 오블완
- 이분탐색
- priority_queue
- Set
- 알고리즘
- BFS
- 깊이우선탐색
Archives
- Today
- Total
안녕 세상아,
[프로그래머스/c++] Lv2 다음 큰 숫자 본문
https://school.programmers.co.kr/learn/courses/30/lessons/12911
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 10진수를 2진수로 바꾸는 함수 작성한다.
2. 2진수 중 1의 갯수를 count 하는 함수 작성한다.
3. 주어진 n을 2진수로 바꾸는 함수에 대입한다.
4. 2진수로 바꾼 후 1의 갯수를 count 하는 함수에 대입한다.
5. for문을 돌리면서 주어진 n보다 크지만 2진수로 바꾼 후 1의 갯수가 같은 자연수를 찾는다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string tenToTwo(int number){
if(number == 0)
return "0";
string bit = "";
while(number != 0){
bit += to_string(number % 2);
number = number / 2;
}
reverse(bit.begin(),bit.end());
return bit;
}
int cnt(string t){
int count = 0;
for(int i = 0; i < t.size(); i++){
if(t[i] == '1')
count++;
}
return count;
}
int solution(int n) {
int answer = 0;
string twoN = tenToTwo(n);
int x = cnt(twoN);
for(int i = n+1;; i++){
string two = tenToTwo(i);
int countOne = cnt(two);
if(countOne == x){
answer = i;
break;
}
}
return answer;
}
'프로그래머스' 카테고리의 다른 글
[프로그래머스/c++] Lv2 피보나치 수 (0) | 2024.08.29 |
---|---|
[프로그래머스/c++] Lv1 시저 암호 (0) | 2024.08.29 |
[프로그래머스/c++] Lv2 숫자의 표현 (0) | 2024.08.26 |
[프로그래머스/c++] Lv1 삼총사 (0) | 2024.08.25 |
[프로그래머스/c++] Lv1 이상한 문자 만들기 (0) | 2024.08.24 |