1010. Radix (25)

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


喵的,因为出差,导致我5天没有做练习!!!

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
/**  数a已知进制,数b未知
 *  这题不难,难在上下限的确定
 *  进制的下限:数b中,各位符号中,最大那个数字加1,例如130,最大数字为3,那么其进制至少为4,因为三进制遇3进1,不应该出现3这个数字,所以130这个数的进制必然是三进制以上(不包括3)
 *  进制的上限:假如数a在十进制下结果为x,那么存在y进制下的数b也等于x,当b为两位数时,y<=x;
 *
 *
 */
#define MAXSIZE 11
#define LLONG long long
#define INF 0x3f3f3f

int charToInt(char ch);
int charToInt(char ch)
{
    if(ch>='0' && ch<='9')
    {
        return ch-'0';
    }
    else if(ch>='a' && ch<='z')
    {
        return ch-'a'+10;
    }
    else
    {
        return -INF;
    }
}

LLONG NumToDec(char *num, LLONG radix);
LLONG NumToDec(char *num, LLONG radix)
{
    int digit = strlen(num);

    int i;
    LLONG rlt = 0;
    for(i=0; i<digit; i++)
    {
        int n = charToInt(num[i]);
        rlt = rlt*radix + n;
    }

    return rlt;
}

LLONG findLowLimit(char *num);
LLONG findLowLimit(char *num)
{
    LLONG max = 0;
    int digit = strlen(num);
    int i;
    for(i=0; i<digit; i++)
    {
        if(max < charToInt(num[i]))
        {
            max = charToInt(num[i]);
        }
    }
    return max+1;
}

LLONG quickCompare(char *num, LLONG radix, LLONG target);
LLONG quickCompare(char *num, LLONG radix, LLONG target)
{
    LLONG sum = 0;
    int digit = strlen(num);
    int i;
    for(i=0; i<digit; i++)
    {
        sum = sum*radix + charToInt(num[i]);
        if(sum>target)
        {
            break;
        }
    }
    if(sum == target)
    {
        return radix;
    }
    else if(sum<target)
    {
        return -INF;
    }
    else
    {
        return INF;
    }
}

LLONG bisection(char *num, LLONG target, LLONG upLimit, LLONG lowLimit);
LLONG bisection(char *num, LLONG target, LLONG upLimit, LLONG lowLimit)
{
    if(upLimit < lowLimit)  /**< 2:upLimit-lowLimit<1; 12、13、6变正确 */
    {
        return -INF;
    }

    LLONG median = (upLimit + lowLimit)/2;

    if(quickCompare(num, median, target) == INF)
    {
        bisection(num, target, median-1, lowLimit);
    }
    else if(quickCompare(num, median, target) == -INF)
    {
        bisection(num, target, upLimit, median+1);/**< 3:median-1; 剩下10、9不对 */
    }
    else
    {
        return  median;
    }
}

int main()
{
    LLONG upLimit;
    LLONG lowLimit;
    LLONG rlt;
    char *a[MAXSIZE], *b[MAXSIZE];
    int tag;
    int givenRadix;
    int i = scanf("%s %s %d %d", a, b, &tag, &givenRadix);

    {
        //printf("%d\n", i);
        //printf("%s %s %d %d", a, b, tag, givenRadix);
    }
    char *c, *d;
    if(tag == 1)
    {
        c = a;
        d = b;
    }
    else
    {
        d = a;
        c = b;
    }

    LLONG target = NumToDec(c, givenRadix);

    if(strcmp(c, d)==0)
    {
    /**< 4:本来以为只要c和d是相等,并且都是1位数的时候输出十进制下的c加1,但是实际是 */
    /**< c和d相等且为1时,输出2,不为1时输出默认给定的radix */
        if(charToInt(c) == 1)
        {
            rlt = 2;
        }
         else
         {
             rlt = givenRadix;
         }

    }
    else
    {
        lowLimit = findLowLimit(d);
        upLimit = lowLimit>target?lowLimit+1:target+1;
        rlt = bisection(d, target, upLimit, lowLimit);
    }

    if(rlt != -INF)
    {
        printf("%lld", rlt);
    }
    else
    {
        printf("Impossible");
    }


    system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值