BOJ : https://www.acmicpc.net/problem/1212

 

1212번: 8진수 2진수

첫째 줄에 8진수가 주어진다. 주어지는 수의 길이는 333,334을 넘지 않는다.

www.acmicpc.net

1373번의 2진수 8진수의 문제를 반대로 생각해보면 된다.

2진수를 3자리씩 표현할 수 있도록 몫과 나머지를 이용하여 문제를 해결한다.

** 꼼꼼하게 풀어보자

 

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
32
33
34
35
#include <cstdio>
#include <string.h>
#include <stack>
#pragma warning(disable:4996)
using namespace std;
 
char str[333337];
stack<int> s;
 
int main() {
    scanf("%s", str);
    int len = strlen(str) - 1;
 
    for (int i = len; i >= 0; i--) {
        int temp = str[i]-'0';
        int cnt = 3;
        if (!temp) s.push(0), cnt--;
        while (temp) {
            cnt--;
            s.push(temp % 2);
            temp /= 2;
        }
        if (cnt) while (cnt--) s.push(0);
    }
 
    while (1) {
        if (s.top() || s.size() == 1break;
        s.pop();
    }
    while (!s.empty()) {
        printf("%d", s.top());
        s.pop();
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 1978번 소수 찾기  (0) 2020.01.13
BOJ 2089번 -2진수  (0) 2020.01.13
BOJ 1373번 2진수 8진수  (0) 2020.01.13
BOJ 2745번 진법 변환  (0) 2020.01.13
BOJ 11005번 진법 변환2  (0) 2020.01.13

+ Recent posts