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

PS:感谢github用户@fs19910227提供的pull request~

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;public class Main {private static String binarySearch(final String value, final BigInteger expect) {//重点在于最大值和最小值的界定 下界大于 所有位中最大值 考虑2 2 1 10 这个caseBigInteger start = BigInteger.valueOf(maxBitValue(value) + 1);//上界不小于下界,不大于expect 考虑200000 1 1 10 这个caseBigInteger end = expect.compareTo(start) < 0 ? start : expect;BigInteger result_radix = null;while (end.compareTo(start) >= 0) {BigInteger middle = start.add(end).divide(BigInteger.valueOf(2));BigInteger ten_value = convert2ten(value, middle);int compare = ten_value.compareTo(expect);if (compare >= 0) {if (compare == 0 && (result_radix == null || middle.compareTo(result_radix) < 0)) {result_radix = middle;}end = middle.subtract(BigInteger.valueOf(1));} else {start = middle.add(BigInteger.valueOf(1));}}return result_radix == null ? "Impossible" : result_radix + "";}private static BigInteger convert2ten(String value, BigInteger radix) {char[] chars = value.toCharArray();BigInteger sum = BigInteger.ZERO;for (char aChar : chars) {int v = char2int(aChar);if (radix.compareTo(BigInteger.valueOf(v)) < 0) {return BigInteger.valueOf(Long.MAX_VALUE);}sum = sum.multiply(radix).add(BigInteger.valueOf(v));}return sum;}private static int maxBitValue(String value) {int max = 0;for (char c : value.toCharArray()) {int i = char2int(c);if (i > max) max = i;}return max;}private static int char2int(char c) {int ret = c - 48;return ret < 10 ? ret : ret - 39;}public static void main(String[] args) throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));String[] split = reader.readLine().split(" ");String v1 = split[0], v2 = split[1];String know_v = v1, unknow_v = v2;BigInteger know_radix = BigInteger.valueOf(Integer.valueOf(split[3]));if ("2".equals(split[2])) {know_v = v2;unknow_v = v1;}//期望值转化为十进制 相当于把输入转化为 N1 N2 1 10BigInteger expect = convert2ten(know_v, know_radix);String s = binarySearch(unknow_v, expect);System.out.println(s);}
}

[Java] 1010. Radix (25)-PAT甲级相关推荐

  1. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  2. 【测试点分析】1010 Radix (25 分)_37行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a pair of positive integers, for example, 6 and 110, can th ...

  3. [Java] 1006. Sign In and Sign Out (25)-PAT甲级

    1006. Sign In and Sign Out (25) At the beginning of every day, the first person who signs in the com ...

  4. PAT Advanced—1010 Radix (25分)

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  5. PAT甲题题解-1010. Radix (25)-二分搜索

    题意:给出n1和n2,以及其中一个数的进制,问另一个数是多少进制的情况下,才会是两个数相等.不存在的话,则输出Impossible 这题思路很简单,但是要考虑的比较多,在简单题里面算是比较好的. 有两 ...

  6. 1040. Longest Symmetric String (25)-PAT甲级真题

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

  7. 2020/7/25 pat甲级45分总结

    额,第一次考甲级,慌慌的,第一题第三题都比较简单,就都写得很快,然后第二题就死磕了..题目中的difference一直当成"差异"来理解,然后一直想不出测试用例的答案是咋的出来的. ...

  8. 1020. Tree Traversals (25) PAT甲级真题

    之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后 ...

  9. 1078. Hashing (25)-PAT甲级真题

    1078. Hashing (25) The task of this problem is simple: insert a sequence of distinct positive intege ...

最新文章

  1. CL_GUI_ALV_GRID 取消右鍵功能
  2. Win docker 桌面版报错error during connect: This error may indicate that the docker daemon is not running
  3. django ORM创建数据库方法
  4. ICSharpCode.SharpZipLib 压缩、解压文件 附源码
  5. 笔记:Java 性能优化权威指南 第6章 Java 应用性能分析技巧
  6. idea控制台搜索功能
  7. 华硕固件默认ip_斐讯刷华硕固件后开启QOS限速的方法
  8. ThingWorx公开课圆满结束
  9. 胶囊网络用于推荐系统问题(MIND,CARP)
  10. 禁止“无法验证发行者,确定要运行此软件吗”提示
  11. Android Camera相机开发示例、Android 开发板 USB摄像头采集、定期拍照、定时拍照,安卓调用摄像头拍照、Android摄像头预览、监控,USB摄像头开发、摄像头监控代码
  12. MFC在对话框中绘制图像
  13. Aegisub打轴学习笔记
  14. 黑色星期五促销活动销售数据分析
  15. 北鲲教程|基于 ABAQUS 的 CFRP 加固钢筋混凝土柱承载能力分析
  16. 中考计算机操作题试题文档,初中信息技术_中考信息技术操作题
  17. mapgis矢量化怎么打分数_MAPGIS制图以及矢量化过程之中一些实用的方法与技巧探讨(1)...
  18. SketchUp Pro 2022 Mac版草图大师
  19. Redis的事务、管道、分布式
  20. 微信小程序组件库(简洁极客)---附源码

热门文章

  1. jquery. Validator验证框架ajax返回json数据
  2. Android调用系统发送短信界面
  3. django-分页器
  4. 连接mysql超过连接次数处理办法
  5. Hibernate 对c3p0配置不支持导致的错误
  6. [MVC4]初识.NET MVC4
  7. [Windows Phone] 如何在 Windows Phone 应用程式制作市集搜寻
  8. 在微博中应用PageRank算法
  9. 整合ext和dwr 的小例子
  10. 安装ISA2004后,加入域时提示:远程过程调用失败且未运行的解决办法