1010. Radix (25)

来源:互联网 发布:php源码分享 编辑:程序博客网 时间:2024/06/06 14:26

1010. Radix (25)

Question
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is “yes”, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number “radix” is the radix of N1 if “tag” is 1, or of N2 if “tag” is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print “Impossible”. If the solution is not unique, output the smallest possible radix.

Sample Input 1:
6 110 1 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible

终于这道题目了
所犯问题如下:
1. radix可以无限大,但是其实是有范围的,具体范围解释在代码注释中
2. 为防止溢出,有一处要做预先处理,注释中有讲

//radix不止2、8、10、16!#include <iostream>#include <string>#include <math.h>using namespace std;long long numberToDecimal(string p, long long radix) {    long long decimal = 0;    long long power = 1;    long long digit = 0;    for (long long i = p.length() - 1; i >= 0; --i) {        if (p[i] >= 'a' && p[i] <= 'z') {            digit = p[i] - 'a' + 10;        }else if (p[i] >= '0' && p[i] <= '9') {            digit = p[i] - '0';        }        decimal += digit * power;        power *= radix;    }    return decimal;}int compare(string x, long long mid, long long target) {    long long x_decimal = 0;    long long power = 1;    long long digit = 0;    for (long long i = x.length() - 1; i >= 0; --i) {        if (x[i] >= 'a' && x[i] <= 'z') {            digit = x[i] - 'a' + 10;        }else if (x[i] >= '0' && x[i] <= '9') {            digit = x[i] - '0';        }        x_decimal += digit * power;        power *= mid;        //必须要,用于处理防止溢出,否则通不过测试        if (x_decimal > target) {            return 1;        }    }    if (x_decimal > target) {        return 1;    } else if (x_decimal < target) {        return -1;    } else {        return 0; //相等了    }}long long binaryDevision(string x, long long lowerR, long long upperR, long long target) {    long long mid = lowerR;    while (lowerR <= upperR) {        int result = compare(x, mid, target);        if (result > 0) {            upperR = mid - 1;        } else if (result < 0) {            lowerR = mid + 1;        } else {            return mid; //找到的出口        }        mid = (lowerR + upperR) / 2;    }    return -1; //没找到}int main(int argc, const char * argv[]) {    //获取输入的数据,用char[]来存    string N[3];    long long tag, radix;    cin >> N[1] >> N[2] >> tag >> radix;    //将已知进制的数转化为十进制    long long target = numberToDecimal(N[tag],radix);    //进行比较了,使用二分法    long long lowerR = 0;    long long upperR = 0;    string aim = N[3 - tag];    for (long long i = aim.length() - 1; i >= 0; --i) {        int digit;        if (aim[i] >= 'a' && aim[i] <= 'z') {            digit = aim[i] - 'a' + 10;        }else if (aim[i] >= '0' && aim[i] <= '9') {            digit = aim[i] - '0';        }        if (digit >= lowerR) {            lowerR = digit + 1;        }    }    if (target > lowerR) {        upperR = target; //在超过target的radix下,aim不可能与target相等了。所以取上界target    } else {        upperR = lowerR;    }    long long result = binaryDevision(aim, lowerR, upperR, target);    if (result == -1) {        cout << "Impossible" << endl;    } else {        cout << result << endl;    }    return 0;}
0 0
原创粉丝点击