본문 바로가기
알고리즘/문자와 문자열

2998 8진수

by tryotto 2020. 1. 4.
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
36
37
38
39
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
 
char arr[105= { 0 };
 
int* toOctat(char* arr, int len, int octL) {    
    int* rst = (int*)calloc(octL, sizeof(int));
    int octIdx = octL - 1;
 
    for (int i = len - 1; i >= 0; ) {
        int tmp = 0;
        for (int j = 0; j < 3; j++) {
            tmp += ((int)(arr[i]) - 48* pow(2, j);
            i--;
 
            if (i < 0break;
        }
        rst[octIdx] = tmp;
        octIdx--;
    }
    return rst;
}
 
int main() {
    scanf("%s"&arr);
 
    int len = strlen(arr);
    int octL = len / 3;
    if (len % 3 != 0)
        octL++;
    
    int* oct = toOctat(arr, len, octL);
 
    for (int i = 0; i < octL; i++) {
        printf("%d", oct[i]);
    }
}
cs


'알고리즘 > 문자와 문자열' 카테고리의 다른 글

2789 유학금지  (0) 2020.01.04
5586 JOI 와 IOI  (0) 2020.01.04
11586 지영 공주님의 마법 거울  (0) 2020.01.04
2804 크로스 만들기  (0) 2020.01.04
5555 반지  (0) 2020.01.04