[백준 2745] 진법 변환 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
임의의 B진법
으로 주어진 수를 10진법
으로 변환하는 문제입니다.
각 자리의 문자를 오른쪽에서부터 차례로 확인하면서,
해당 문자의 값에 B의 거듭제곱을 곱해 모두 더한 값을 출력하면 됩니다.
접근법
각 자리의 문자를 가장 오른쪽(1의 자리)부터 차례대로 확인하면서,
해당 문자가 의미하는 값을 기수 B
의 자리값(거듭제곱)과 곱하여 모두 더하면,
B진법 수를 10진법으로 변환한 값이 됩니다.
예를 들어, 진법이 B
일 때 수 N = d_0 d_1 ... d_(n-1)
은 다음과 같은 수식으로 계산됩니다:
이 원리를 코드로 그대로 구현하면 됩니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
class Program {
static void Main() {
var input = Console.ReadLine().Split();
string s = input[0];
int b = int.Parse(input[1]);
int res = 0;
for (int i = 0; i < s.Length; i++) {
int digit = s[i] >= 'A' ? s[i] - 'A' + 10 : s[i] - '0';
res = res * b + digit;
}
Console.WriteLine(res);
}
}
C++
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
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s; int b; cin >> s >> b;
int n = s.size();
vi pow(n);
pow[0] = 1;
for (int i = 1; i < n; i++)
pow[i] = pow[i - 1] * b;
int ans = 0;
for (int i = 0; i < n; i++) {
int d = isalpha(s[i]) ? s[i] - 55 : s[i] - 48;
ans += d * pow[n - 1 - i];
}
cout << ans << "\n";
return 0;
}