1010. Radix (25)(java版)

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的范围,并用剪枝方法减少过程运算量。已有的一些方法都是用C++来写的,这里在java里处理有可能很大的数用到的是BigInteger。

几个注意的点:

  1. 上下界的确定。 下界自然是最大位+1,上界的确定有点奇怪,原本是 max(下界,十进制值)+1,但这样会出现通不过第19个测试点的情况,尝试着将上界改成 max(50,十进制值)+1,结果就通过了,不知道为什么

  2. 原本我用于搜索的low,high, mid值是int类型的,但一直通不过第7个测试点,后来将其类型改为BigInteger值成功了,猜测该测试点的测试集挺大的

  3. 对于两个数相同的情况,不像前文中说的和输入的radix即可,只需按最小的输出也可以。 还有经测试 0 0 1 10 这种情况,output 1 也未尝不可。

代码:

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n1 = sc.next(), n2 = sc.next();
        int tag = sc.nextInt(), radix = sc.nextInt();
        if (tag == 2) {
            String tmp = n1;
            n1 = n2;
            n2 = tmp;
        }
        BigInteger num1 = str2Num(n1, radix);
        BigInteger low=findBiggest(n2);
        BigInteger minRadix=BigInteger.ZERO;
        BigInteger left=low.add(BigInteger.ONE);
        BigInteger right=num1.max(BigInteger.valueOf(50)).add(BigInteger.ONE);
        while(left.compareTo(right)!=1){
            BigInteger mid=left.add(right).divide(BigInteger.valueOf(2));
            BigInteger tmpValue=BigInteger.ZERO;
            int flag=-1;
            for(int i=0;i<n2.length();i++){
                tmpValue=tmpValue.multiply(mid).add(char2Num(n2.charAt(i)));
                if(tmpValue.compareTo(num1)==1){
                    flag=1;
                    break;
                }else if(tmpValue.compareTo(num1)==0){
                    flag=0;
                    break;
                }
            }
            if(flag==1)
                right=mid.subtract(BigInteger.ONE);
            else if(flag==-1)
                left=mid.add(BigInteger.ONE);
            else{
                minRadix=mid;
                right=mid.subtract(BigInteger.ONE);
            }

        }
        if(minRadix.equals(BigInteger.ZERO))
            System.out.println("Impossible");
        else
            System.out.println(minRadix);

        sc.close();

    }
    private static BigInteger findBiggest(String str) {
        BigInteger max=BigInteger.ZERO;
        for(int i=0;i<str.length();i++){
            if(max.compareTo(char2Num(str.charAt(i)))==-1)
                max=char2Num(str.charAt(i));
        }
        return max;
    }
    private static BigInteger str2Num(String str, int radix) {
        BigInteger value=BigInteger.ZERO;
        for(int i=0;i<str.length();i++){
            value=value.multiply(BigInteger.valueOf(radix)).add(char2Num(str.charAt(i)));
        }
        return value;
    }
    private static BigInteger char2Num(Character c) {
        BigInteger value;
        if (c >= '0' && c <= '9')
            value = BigInteger.valueOf(c - '0');
        else
            value = BigInteger.valueOf(c - 'a' + 10);
        return value;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值