[BOJ][구현][브4][1550] 16진수

문제 링크

문제링크

첫 번째 풀이

알고리즘

주어지는 16진수의 길이가 최대 6입니다. 예를 들어 FFFFFF가 입력될 수 있습니다.

정답 코드

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 long long ll;
int main()
{
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(0); cin.tie(0);

    string s;
    cin >> s;
    ll ans = 0;

    for (int i = s.size()-1; i>=0 ; --i) {
        int coff = 0;
        if ('A' <= s[i]) {
            coff = s[i] - 'A' + 10;
        }
        else if ('0' <= s[i]) {
            coff = s[i] - '0';
        }
        ans += coff * pow(16, s.size() - 1 - i);
    }
    cout << ans;
    return 0;
}

Success Notice: 수고하셨습니다. :+1:

Leave a comment