Algorithm/Problem Solving
2020. 5. 12. 23:14
(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)
문제 링크
- https://www.acmicpc.net/problem/2089
사용 알고리즘
-
풀이
- 나머지가 음수가 되지 않도록 계산
소스 코드
- https://github.com/moomini/algorithm/blob/master/boj/2089.cpp
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
int n; scanf("%d", &n);
vector<int> v;
if (n == 0) printf("0");
while (n != 0) {
int remainder = n % (-2);
n /= (-2);
if (remainder == -1) {
remainder = 1;
++n;
}
v.push_back(remainder);
}
reverse(v.begin(), v.end());
int size = v.size();
for (int i = 0; i < size; ++i) printf("%d", v[i]);
printf("\n");
return 0;
}
|
'Algorithm > Problem Solving' 카테고리의 다른 글
[BOJ/1978] 소수 찾기 (0) | 2020.05.13 |
---|---|
[BOJ/11576] Base Conversion (0) | 2020.05.13 |
[BOJ/1212] 8진수 2진수 (0) | 2020.05.12 |
[BOJ/1373] 2진수 8진수 (0) | 2020.05.11 |
[BOJ/2745] 진법 변환 (0) | 2020.04.29 |