PAT-1010 Radix

本文介绍了一个算法问题,即给定两个正整数,如何通过转换基数使它们相等。通过二分查找的方法,高效地找到满足条件的基数,解决了基数查找的难题。

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

算法说明:

已知一个数和其基数,求另一个数的基数使得这两个数相等。数字表示使用[0-9a-z],很容易看出基数的范围是[2-36],如果已知数很大,基数是会超出36的,可能会很大很大,用long long 来存储这个基数,如果你用暴力遍历查找基数,时间会超时,可以用二分查找。

#include <iostream>
#include <cstring>
using namespace std;
#define Max 3
// 转成十进制 
long long toDecimal(char *N,long long radix){
    long long decimal=0;
    for(int i=0;i<strlen(N);i++){
        if(N[i]<58)
            decimal=decimal*radix+(N[i]-48);   
        else
            decimal=decimal*radix+(N[i]-87); // a-z ,10-35 呵呵 
    }
    return decimal;
}
//找出串中最大值 
int maxValueStr(char *N){
    int max=0;
    for(int i=0;i<strlen(N);i++)
        if(N[i]<58&&N[i]-48>max)
            max=N[i]-48;
        else if(N[i]-87>max)
            max=N[i]-87; 
    if(max>1)
        return max;
    else
        return 1;
}
int compare(char *N,long long radix,long long target){
    long long decimal=0;
    for(int i=0;i<strlen(N);i++){
        if(N[i]<58)
            decimal=decimal*radix+(N[i]-48);
        else
            decimal=decimal*radix+(N[i]-87);
        if(decimal>target||decimal<0)
            return 1; 
    }
    if(decimal>target)
        return 1;
    else if(decimal<target)
        return -1;
    else
        return 0;
}
long long binarySearch(long long target,char *N,long long low,long long high){
    long long mid=low;
    
    while(low<=high){
        if(compare(N,mid,target)==1)
            high=mid-1;
        else if(compare(N,mid,target)==-1)
            low=mid+1;
        else
            return mid;
        mid=(low+high)/2;
    }
    return -1;
}   
int main(int argc, char* argv[])
{
    char *N[Max];
    
    long long radix,target;
    int tag;
    for(int i=0;i<Max;i++)
        N[i]=new char[10]();
    
    cin >> N[1] >> N[2] >> tag >> radix;
    
    target=toDecimal(N[tag],radix); //目标数转成十进制比较 
    tag=(tag==1)? 2:1;
    long long max=(long long)maxValueStr(N[tag])+1;  // 出现最大值f 进制为16 +1 
    
    if(target<=max){
        long long result=binarySearch(target,N[tag],max,36);
        if(result==-1)
            cout<<"Impossible";
        else
            cout<<result;
    }else{
        long long result=binarySearch(target,N[tag],max,target+1);
        if(result==-1)
            cout<<"Impossible";
        else
            cout<<result;
    }
    return 0;
}

2020考研打卡第十三天,星辰之变,骄阳岂是终点。

我要争一口气,不是想证明我了不起,我是要告诉大家,我失去的东西一定要拿回来。

转载于:https://www.cnblogs.com/chengdalei/p/10739713.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值