https://www.acmicpc.net/problem/2745
문자열과 숫자를 입력받아, 해당 문자열을 주어진 숫자 진법으로 바꾸는 문제.
#include <iostream>
#include <string>
#include <map>
#include <cmath>
using namespace std;
int main(){
string n;
int b;
cin >> n >> b;
map<char, int> m;
for (int i=0;i<10;i++){
m.insert({'0'+i, i});
}
for (int i=10;i<36;i++){
m.insert({'A'+i-10, i});
}
int exp = 0;
int answer = 0;
for (int i=n.length()-1;i>=0;i--){
answer += m.find(n[i])->second * pow(b,exp);
exp+=1;
}
cout << answer;
}
map을 통해 0~Z까지 값을 매핑시켜, 입력받은 문자열을 숫자로 변환시킨 후, 이를 각 문자열의 자릿수와 진법에 맞게 제곱한 값을 곱해주어 답을 구한다.
'알고리즘 > 백준' 카테고리의 다른 글
백준 2720번 세탁소 사장 동혁 [C++] (1) | 2023.11.26 |
---|---|
백준 11005 진법 변환 2 [C++] (0) | 2023.11.26 |
백준 2563번 색종이 [C++] (1) | 2023.11.25 |
백준 10798번 세로읽기 [C++] (0) | 2023.11.25 |
백준 25206번 너의 평점은 [C++] (0) | 2023.11.23 |