Algorithm/Problem Solving 2020. 5. 12. 23:00

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

문제 링크

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

 

사용 알고리즘

 

풀이

- 8진수 1자리당 2진수 3자리 (첫 번째 수는 1로 시작)

 

소스 코드

- https://github.com/moomini/algorithm/blob/master/boj/1212.cpp

#include <cstdio>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
char input[333337];
 
int main(void) {
    scanf("%s", input);
    int len = strlen(input);
    for (int i = 0; i < len; ++i) {
        int num = input[i] - '0';
        int temp[3];
        for (int j = 0; j < 3++j) {
            temp[j] = num % 2;
            num /= 2;
        }
        reverse(temp, temp + 3);
        if (i == 0) {
            int flag = 0;
            for (int j = 0; j < 3++j) {
                if (flag == 1) {
                    printf("%d", temp[j]);
                }
                if (flag == 0 && temp[j] == 1) {
                    flag = 1;
                    printf("%d", temp[j]);
                }
            }
            if (flag == 0printf("0\n");
        }
        else {
            for (int j = 0; j < 3++j) {
                printf("%d", temp[j]);
            }
        }
 
    }
    printf("\n");
    return 0;
}

Colored by Color Scripter

cs

'Algorithm > Problem Solving' 카테고리의 다른 글

[BOJ/11576] Base Conversion  (0) 2020.05.13
[BOJ/2089] -2진수  (0) 2020.05.12
[BOJ/1373] 2진수 8진수  (0) 2020.05.11
[BOJ/2745] 진법 변환  (0) 2020.04.29
[BOJ/11005] 진법 변환 2  (0) 2020.04.27
posted by DevMoomin
: