Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <exception>
- #include <iostream>
- #include <string>
- typedef long long ll;
- int main2() {
- int n = 0;
- int base = 10;
- std::cin >> n >> base;
- std::string result;
- while (n > 0) {
- char c = char(n % base) + '0';
- n /= base;
- result += c;
- }
- std::reverse(result.begin(), result.end());
- std::cout << result << std::endl;
- }
- int CharToInt(char c) {
- if (c >= '0' && c <= '9') return c - '0';
- if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
- if (c >= 'a' && c <= 'z') return c - 'a' + 10;
- throw std::runtime_error("Very Bad Symbol");
- }
- ll Str2Int(std::string& s, int base) {
- ll result = 0;
- // Схема Горнера.
- // result = (((s[0]*10 + s[1])* 10 + s[2])*10 + s[3])*10 + ... + s[l-1];
- for (int i = 0; i < s.length(); ++i) {
- result = result * base + CharToInt(s[i]);
- }
- return result;
- }
- int main() {
- std::string s;
- int base = 10;
- std::cin >> s >> base;
- try {
- std::cout << Str2Int(s, base) << std::endl;
- } catch (std::runtime_error& e) {
- std::cout << e.what();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment