Pat 1010 Radix

Source: Internet
Author: User

After a day, I felt like I was not doing anything. I wanted to make up for my questions. I was thinking about what to do. My roommate said I 'd try Pat 1010. I skipped this question, it seems that this pass rate (0.07) is a bit exaggerated. The question is "know one number". Calculate the base number of the other number to make the two numbers equal. Naturally, considering that binary search is used to determine the base number, numbers represent [0-9a-z]. This TMD makes it easy to think that the range of the base number is 1 ~ The value range is greater than 36. If this value is not taken into account, a typical score is 19. However, if the base number is between [1, 36], the search range is too small, and direct brute force traversal is also acceptable, so the scope of the search is expanded without sound. In short, it is a pitfall question, old women love this. The following code is provided:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;char char2num[128];int remove_leading_zero(char* str) {    if (str[0] == ‘\0‘) return 0;    int ri = 0, wi = 0;    while (str[ri] == ‘0‘ || str[ri] == ‘+‘ || str[ri] == ‘-‘) ri++;    int len = 0;    while ((str[wi++] = str[ri++]) != ‘\0‘) len++;    if (len == 0) {        str[0] == ‘0‘;        str[++len] = ‘\0‘;    }    return len;}long long value(const char* str, int len, long long radix) {    long long ret = 0;    long long r = 1;    for (int i=len - 1; i>=0; i--) {        int digit = char2num[str[i]];        // we should check the number validation        if (digit >= radix) return -1;        ret += r * digit;        r *= radix;    }    return ret;}int inc_cmp(char* str, int len, long long radix, long long target){    long long v = 0;    long long r = 1;    for (int i=len - 1; i>=0; i--) {        int digit = char2num[str[i]];        v += r * digit;        r *=radix;        if (v > target) {            return 1;        }    }    if (v == target) {        return 0;    } else {        return -1;    }}long long binary_search(char* str, int len, long long lo, long long hi, long long target){    long long mid;    lo = lo - 1;    hi = hi + 1;    while(lo + 1< hi){        mid = (lo + hi) / 2;        int res = inc_cmp(str, len, mid, target);        if(res > 0) {            hi = mid;        } else if(res < 0) {            lo = mid;        } else {            return mid;        }    }    return -1;}int main() {    // init char2num lookup table    for (int i=0; i<10; i++) char2num[‘0‘ + i] = i;    for (int i=‘a‘; i<=‘z‘; i++) char2num[i] = i - ‘a‘ + 10;    char num1[16] = {‘\0‘};    char num2[16] = {‘\0‘};    char *pnum1 = num1, *pnum2 = num2;    int tag = 0;    long long bradix = 0;    scanf("%s%s%d%ld", num1, num2, &tag, &bradix);    // we always assure that bradix is the radix of pnum1    // and pnum2 is which we should guess its radix    if (tag != 1) {        pnum1 = num2;        pnum2 = num1;    }        int n1len = remove_leading_zero(pnum1);    int n2len = remove_leading_zero(pnum2);        long long n1 = value(pnum1, n1len, bradix);        bool is_same = !strcmp(pnum1, pnum2);    if(is_same) {        if (n1len > 1) {            // must be same radix, if digits more than one            printf("%d\n", bradix);        } else {            // only one digit, so choose a smallest valid radix            printf("%d\n", n1 + 1);        }        return 0;    }     long long lo = 0;    for (int i=0; i<n2len; i++) {        int d = char2num[pnum2[i]];        if (d + 1> lo) lo = d + 1;    }    long long hi = n1 > lo ? n1 : lo;    int res = binary_search(pnum2, n2len, lo, hi, n1);    if (res < 0) {        printf("Impossible\n");    } else {        printf("%d", res);    }    return 0;}

 

Pat 1010 Radix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.