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

+ Recent posts