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

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

 

1373번: 2진수 8진수

첫째 줄에 2진수가 주어진다. 주어지는 수의 길이는 1,000,000을 넘지 않는다.

www.acmicpc.net

 

2진수를 8진수로 변환하는 문제

8진수가 0~7의 범위를 표현할 수 있다는 점을 이용하면

0~7 은 2진수로 3자리 000~111 로 표현할 수 있다.

그러므로 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
36
#include <iostream>
#include <string>
#include <stack>
using namespace std;
 
stack<int> s;
 
int main() {
    ios::sync_with_stdio(0);
    int cnt = 0, total = 0;
    bool check = true;
    string str;
    cin >> str;
    for (int i = str.size() - 1; i >= 0; i--) {
        if (cnt == 3) {
            check = false;
            s.push(total);
            cnt = 0;
            total = 0;
        }
        if (str[i] == '1') {
            check = true;
            int temp = 1;
            for (int k = 0; k < cnt; k++) {
                temp *= 2;
            }
            total += temp;
        }
        cnt++;
    }
    if (check) s.push(total);
    while (!s.empty()) {
        cout << 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 2089번 -2진수  (0) 2020.01.13
BOJ 1212번 8진수 2진수  (0) 2020.01.13
BOJ 2745번 진법 변환  (0) 2020.01.13
BOJ 11005번 진법 변환2  (0) 2020.01.13
BOJ 9613번 GCD 합  (0) 2020.01.12

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

 

2745번: 진법 변환

B진법 수 N이 주어진다. 이 수를 10진법으로 바꿔 출력하는 프로그램을 작성하시오. 10진법을 넘어가는 진법은 숫자로 표시할 수 없는 자리가 있다. 이런 경우에는 다음과 같이 알파벳 대문자를 사용한다. A: 10, B: 11, ..., F: 15, ..., Y: 34, Z: 35

www.acmicpc.net

진법변환2 와는 반대로 임의의 값과 쓰인 진법이 주어지면 10진법으로 변환하는 문제이다.

자리수당 쓰인 진법만큼 제곱하여 현재값과 곱한 값을 더해가면 쉽게 유추해낼수있다.

 

 

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
#include <iostream>
#include <string>
using namespace std;
 
int arr[26];
 
int main() {
    string str;
    int X, cnt = 0, total = 0;
    cin >> str >> X;
 
    for (int i = 0; i < 26; i++) arr[i] = 10 + i;
 
    for (int i = str.size() - 1; i >= 0; i--) {
        int temp = 1;
        int cntt = cnt++;
        while (cntt--) temp *= X;
        if (str[i] >= 'A')
            temp *= arr[str[i] - 'A'];
        else
            temp *= str[i] - '0';
        total += temp;
    }
 
    cout << total;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

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

BOJ 1212번 8진수 2진수  (0) 2020.01.13
BOJ 1373번 2진수 8진수  (0) 2020.01.13
BOJ 11005번 진법 변환2  (0) 2020.01.13
BOJ 9613번 GCD 합  (0) 2020.01.12
BOJ 1850번 최대공약수  (0) 2020.01.12

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

 

11005번: 진법 변환 2

10진법 수 N이 주어진다. 이 수를 B진법으로 바꿔 출력하는 프로그램을 작성하시오. 10진법을 넘어가는 진법은 숫자로 표시할 수 없는 자리가 있다. 이런 경우에는 다음과 같이 알파벳 대문자를 사용한다. A: 10, B: 11, ..., F: 15, ..., Y: 34, Z: 35

www.acmicpc.net

진법변환의 간단한 방법은

예를 들어 16을 16진법으로 바꾸고자 할때

16을 16으로 나누면 몫은 1, 나머지는 0이 된다. 나머지 0 은 스택에 넣고

몫 1을 다시 16으로 나누면 몫은 0, 나머지는 1이 된다. 나머지 1은 스택에 넣고

몫이 0이므로 계산을 종료하고 스택을 그대로 출력해주면 10이 된다.

 

 

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
#include <iostream>
#include <stack>
using namespace std;
 
stack<char> s;
 
int main() {
    int N, X;
    cin >> N >> X;
 
    while (N) {
        int cur = N % X;
        N /= X;
        if (X > 9) {
            if (cur > 9) s.push((cur - 10+ 'A');
            else s.push(cur);
        }
        else {
            s.push(cur);
        }
    }
 
    while (!s.empty()) {
        if (s.top() >= 'A'cout << s.top(), s.pop();
        else cout << s.top()-'\0', s.pop();
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

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

BOJ 1373번 2진수 8진수  (0) 2020.01.13
BOJ 2745번 진법 변환  (0) 2020.01.13
BOJ 9613번 GCD 합  (0) 2020.01.12
BOJ 1850번 최대공약수  (0) 2020.01.12
BOJ 1934번 최소공배수  (0) 2020.01.12

+ Recent posts