Algorithm/Problem Solving

[BOJ/2745] 진법 변환

DevMoomin 2020. 4. 29. 22:36

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

 

문제 링크

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

 

사용 알고리즘

 

풀이

- B ^ k를 곱하면서 더함

 

소스 코드

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

#include <iostream>
#include <string>
 
using namespace std;
 
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    string n; int b; cin >> n >> b;
    int ans = 0;
    int len = n.length(), temp = 1;
    for (int i = 0; i < len; ++i) {
        int num;
        if (n[i] >= '0' && n[i] <= '9') num = n[i] - '0';
        else num = n[i] - 55;
        ans *= b;
        ans += num;
    }
    cout << ans << '\n';
    return 0;
}

Colored by Color Scripter

cs