zoukankan      html  css  js  c++  java
  • 1010

    #include <iostream>
    #include <cctype>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    long long convert(string n, long long radix) {
        long long sum = 0;
        int index = 0, temp = 0;
        for (auto it = n.rbegin(); it != n.rend(); it++) {
            temp = isdigit(*it) ? *it - '0' : *it - 'a' + 10;// - 'a' + 10 transform letter to 10radix number;-'0' transform string to number;(a-z represent the decimal numbers 10-35.)
            sum += temp * pow(radix, index++);
        }
        return sum;
    }
    long long find_radix(string n, long long num) {
        char it = *max_element(n.begin(), n.end());
        long long low = (isdigit(it) ? it - '0' : it - 'a' + 10) + 1;
        long long high = max(num, low);
        while (low <= high) {
            long long mid = (low + high) / 2;
            long long t = convert(n, mid);
            if (t < 0 || t > num) //if t is negative number,that mean t out of long long boundary.
                high = mid - 1;
            else if (t == num) return mid;
            else low = mid + 1;
        }
        return -1;
    }
    int main() {
        string n1, n2;
        long long tag = 0, radix = 0, result_radix;
        cin >> n1 >> n2 >> tag >> radix;
        result_radix = tag == 1 ? find_radix(n2, convert(n1, radix)) : find_radix(n1, convert(n2, radix));
        if (result_radix != -1) {
            printf("%lld", result_radix);
        }
        else {
            printf("Impossible");
        }
        return 0;
    }
  • 相关阅读:
    Redis设计与实现第一部分:第5章:Redis 跳跃表
    根据临时表修改主表的某字段数据根据主表的主键
    Redis设计与实现第一部分:第2章:简单动态字符串SDS
    Redis
    MySQL的访问控制与用户管理
    MySQL字符集和语言的基础知识
    生成日志文件
    Python进阶09 动态类型
    Python进阶08 异常处理
    Python进阶07 函数对象
  • 原文地址:https://www.cnblogs.com/masayoshi/p/10848297.html
Copyright © 2011-2022 走看看