PAT:二分查找

1010. 进制(Radix)

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

思路
这道题的边界条件有很多。

  • 首先,radix的取值范围其实是[2,sum+1],其中,sum是已知某进制的数的十进制值。因此,为了节约时间,应该用二分查找。
  • 每次读取一位digit后,都要判断digit<radix 是否成立,因为如果不成立,就不合法。
  • 计算第二个数的sum2时,还要考虑到sum2溢出的情况,if(sum<0) return -1; ,说明取的radix太大。
  • 还要考虑第二个数只有一位,并且n2[0]==sum 时,最小进制取sum+1
#include<stdlib.h>
#include<limits.h>
#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<stack>

using namespace std;

int main()
{
    string n1,n2;
    int tag=0;
    long long radix=0;
    long long res;
    solusion sol;
    cin>>n1>>n2>>tag>>radix;
    if(tag==1) {
        res=sol.Res(n1,radix);
        sol.findRadix(n2,res);
    }
    else if(tag==2) {
        res=sol.Res(n2,radix);
        sol.findRadix(n1,res);
    }   
return 0;
}

class solusion{
public:
    long long res1,res2;

    //返回以radix为进制的数字n的实际十进制值
    long long Res(string n,long long radix) {
        long long res=0;
        int length=n.size();
        int index=0;
        for(index=0;index<length;index++) {
            int oneDigit=transform(n[index]);
            if(oneDigit>=radix) return -2;//一位数字就超过了进制,不合法
            res=radix*res+oneDigit;
            if(res<0) return -1;//溢出
        }
        return res;
    }
    //将字符值转换成十进制值
    int transform(char c) {
        if(c>='0' && c<='9') return c-'0';
        return ( c-'a'+10);
    }
    //根据n和另一个数的十进制值,求n的正确进制
    void findRadix(string n,long long res) {
        long long low=2;
        long long high=res+1;
        long long sum;
        //如果n只有一位,且n等于res,那么进制就是res+1
        if(n.size()==1 && transform(n[0])==res) {
            cout<<res+1<<endl;
            return;
        }
        while(low<=high) {
            sum=Res(n,(low+high)/2);
            //如果sum溢出或者sum大于res,说明进制取得太大了
            if(sum==-1 || sum>res)
                high=(low+high)/2-1;
            else if(sum<res)
                low=(low+high)/2+1;
            else if(sum==res) {
                cout<<(low+high)/2<<endl;
                return;
            }
        }
        cout<<"Impossible"<<endl;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值