PAT1010甲级 java

2020/7/12

问题

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 N​1​​ and N​2​​, 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.
给出一个数和其基数,求另一个数的基数
知识点
二分法

java

import java.io.IOException;
import java.math.BigInteger;
import java.util.*;

public class Main {

    static Scanner sc=new Scanner(System.in);
    static String a=sc.next();//N1
    static String b=sc.next();//N2
    static int c=sc.nextInt();//tag
    static String d=sc.next();//radix
    static final int BUFFER_SIZE=8192*25;
    public static void main(String[] args) throws IOException {
        String n1=c==1?a:b;
        String n2=c==1?b:a;
        final BigInteger N=converts(n1,new BigInteger(d));
        int maxBit=0;
        for(int i=0;i<n2.length();i++){
            char ch=n2.charAt(i);
            int val=(ch>='0' && ch<='9')? ch-'0':ch-'a'+10;
            maxBit=Math.max(val,maxBit);
        }
        BigInteger low=BigInteger.valueOf(maxBit+1);
        BigInteger high=N.compareTo(low)<0?low:N;
        BigInteger res=null;
        //二分法
        while (high.compareTo(low)>=0){
            BigInteger mid=low.add(high).divide(BigInteger.valueOf(2));
            BigInteger midTenval=converts(n2,mid);
            if(midTenval.compareTo(N)>=0){
                if(midTenval.compareTo(N)==0 && (res==null || mid.compareTo(res)<0)){
                    res=mid;
                }
                high=mid.subtract(BigInteger.valueOf(1));
            }else {
                low=mid.add(BigInteger.valueOf(1));
            }
        }
        if(res==null) System.out.println("Impossible");
        else System.out.println(res);
    }
    //将一个radix进制下的数valStr转化为10进制下的表示
    public static BigInteger converts(String val, BigInteger radix){
        char[] valArr=val.toCharArray();
        BigInteger sum=BigInteger.ZERO;
        for(int i=0;i<valArr.length;i++){
            int v=valArr[i]-'0'<10? valArr[i]-'0':valArr[i]-'a'+10;
            if(radix.compareTo(BigInteger.valueOf(v))<0){
                return BigInteger.valueOf(Long.MAX_VALUE);
            }
            sum=sum.multiply(radix).add(BigInteger.valueOf(v));
        }
        return sum;
    }
}


:详情请参见https://blog.csdn.net/qq_26073557/article/details/105192890

你可能感兴趣的:(PAT甲级)