c_cpp uva10093

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp uva10093相关的知识,希望对你有一定的参考价值。

#include <array>
#include <iostream>
#include <string>
using namespace std;

array<int, 128> create_digit_table()
{
    array<int, 128> table;
    int value = 0;
    for (const auto ch : string("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")) table[ch] = value++;
    return table;
}

inline int get_digit_value(char ch)
{
    static const auto table = create_digit_table();
    return isalnum(ch) ? table[ch] : -1;
}

inline bool test_base(const string &str, int base)
{
    int remain = 0, divisor = base - 1;
    for (const auto ch : str)
    {
        auto value = get_digit_value(ch);
        if (value >= base) return false;
        if (value < 0) continue;

        remain *= base;
        remain += value;
        remain %= divisor;
    }
    return (remain == 0);
}

int smallest_base(const string &str)
{
    for (size_t base = 2; base <= 62; base++)
    {
        if (test_base(str, base)) return base;
    }
    return 0;
}

int main()
{
    string str;
    while (cin >> str)
    {
        auto base = smallest_base(str);
        cout << (base >= 2 ? to_string(base) : "such number is impossible!") << endl;
    }
    return 0;
}

以上是关于c_cpp uva10093的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp uva12307

c_cpp uva681

c_cpp uva10101

c_cpp uva10107

c_cpp uva591

c_cpp uva12015