[PAT] 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



 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStreamReader;
 4 import java.math.BigInteger;
 5 
 6 /**
 7  * @Auther: Xingzheng Wang
 8  * @Date: 2019/2/18 23:08
 9  * @Description: pattest
10  * @Version: 1.0
11  */
12 public class PAT1010 {
13     public static void main(String[] args) throws IOException {
14         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
15         String[] split = reader.readLine().split(" ");
16         String v1 = split[0], v2 = split[1];
17         String know_v = v1, unknow_v = v2;
18         BigInteger know_radix = BigInteger.valueOf(Integer.valueOf(split[3]));
19         if ("2".equals(split[2])) {
20             know_v = v2;
21             unknow_v = v1;
22         }
23         //期望值转化为十进制 相当于把输入转化为 N1 N2 1 10
24         BigInteger expect = convert2ten(know_v, know_radix);
25         String s = binarySearch(unknow_v, expect);
26         System.out.println(s);
27     }
28     //转化为十进制
29     private static BigInteger convert2ten(String value, BigInteger radix) {
30         char[] chars = value.toCharArray();
31         BigInteger sum = BigInteger.ZERO; //
32         for (char aChar : chars) {
33             int v = char2int(aChar);
34             if (radix.compareTo(BigInteger.valueOf(v)) < 0) {
35                 return BigInteger.valueOf(Long.MAX_VALUE);
36             }
37             sum = sum.multiply(radix).add(BigInteger.valueOf(v));
38         }
39         return sum;
40     }
41     //char转换成int, ASCII中'0' = 48; 'A' = 65;'a' = 97;    'a' - '0' - 39= 97 - 48 - 39 = 10
42     private static int char2int(char c) {
43         int ret = c - '0';
44         return ret < 10 ? ret : ret - 39;
45     }
46 
47         //重点在于最大值和最小值的界定 下界大于 所有位中最大值 考虑2 2 1 10 这个case
48         //上界不小于下界,不大于expect 考虑200 1 1 10 这个case
49     private static String binarySearch(final String value, final BigInteger expect) {
50         BigInteger start = BigInteger.valueOf(maxBitValue(value) + 1); //下界 value数字中最大位+1
51         BigInteger end = expect.compareTo(start) < 0 ? start : expect; //上界 expect
52         BigInteger result_radix = null;
53         while (end.compareTo(start) >= 0) {
54             BigInteger middle = start.add(end).divide(BigInteger.valueOf(2));
55             BigInteger ten_value = convert2ten(value, middle);
56             int compare = ten_value.compareTo(expect);
57             if (compare >= 0) {
58                 if (compare == 0 && (result_radix == null || middle.compareTo(result_radix) < 0))
59                     result_radix = middle;
60                 end = middle.subtract(BigInteger.valueOf(1));  //subtract相减
61             } else {
62                 start = middle.add(BigInteger.valueOf(1));
63             }
64         }
65         return result_radix == null ? "Impossible" : result_radix + "";
66     }
67     //求其中value各位数字中最大数字
68     private static int maxBitValue(String value) {
69         int max = 0;
70         for (char c : value.toCharArray()) {
71             int i = char2int(c);
72             if (i > max)
73                 max = i;
74         }
75         return max;
76     }
77 }
原文地址:https://www.cnblogs.com/PureJava/p/10497972.html