-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_arithmetic.cc
34 lines (29 loc) · 832 Bytes
/
base_arithmetic.cc
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
27
28
29
30
31
32
33
34
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
auto isHexChar(char c) noexcept -> bool { return c >= 'A' && c <= 'F'; }
auto charToDigit(char c) noexcept -> int {
if (isHexChar(c)) {
return 10 + (c - 'A');
}
return c - '0';
}
auto findBase(const std::string& str) noexcept -> int {
return charToDigit(
*std::max_element(std::begin(str), std::end(str), [](char lhs, char rhs) {
return charToDigit(lhs) < charToDigit(rhs);
})) + 1;
}
auto bestConversion(const std::string& str) noexcept -> long long {
return std::stoll(str, nullptr, findBase(str));
}
int main() {
auto line = std::string{};
getline(std::cin, line);
auto x = bestConversion(line);
getline(std::cin, line);
auto y = bestConversion(line);
std::cout << x + y << '\n';
}