PAT甲级练习1010. Radix (25)
来源:互联网 发布:微信一键传图软件 编辑:程序博客网 时间:2024/05/24 06:06
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 10Sample Output 1:
2Sample Input 2:
1 ab 1 2Sample Output 2:
Impossible
这题参考了别人的思路,参见 http://www.liuchuo.net/archives/2458 ,
http://blog.csdn.net/apie_czx/article/details/45370503,
http://www.cnblogs.com/549294286/p/3571604.html
就是使用二分法来减少搜索radix的范围,并用剪枝方法减少过程运算量。已有的一些方法都是用C++来写的,这里在java里处理有可能很大的数用到的是BigInteger。
几个注意的点:
1. 上下界的确定。 下界自然是最大位+1,上界的确定有点奇怪,原本是 max(下界,十进制值)+1,但这样会出现通不过第19个测试点的情况,尝试着将上界改成 max(1000,十进制值)+1,结果就通过了,不知道为什么
2. 原本我用于搜索的low,high, mid值是int类型的,但一直通不过第7个测试点,后来将其类型改为BigInteger值成功了,猜测该测试点的测试集挺大的
3. 对于两个数相同的情况,不像前文中说的和输入的radix即可,只需按最小的输出也可以。 还有经测试 0 0 1 10 这种情况,output 1 也未尝不可
PS. 为什么提交的时候总是出现返回非零的情况,真的好烦啊
import java.math.BigInteger;import java.util.Scanner;public class Main {private static BigInteger low=BigInteger.valueOf(-1), high, mid;private static BigInteger findRadix(String str, BigInteger decimalValue){for(int i=0; i<str.length(); i++){if(ch2num(str.charAt(i)).compareTo(low) == 1){low = ch2num(str.charAt(i));}}BigInteger radix = BigInteger.ZERO;low = low.add(BigInteger.ONE);high = decimalValue.max(BigInteger.valueOf(1000)).add(BigInteger.ONE);//high = low.max(decimalValue).add(BigInteger.ONE);这样写的话在测试点19通不过,不知道为什么while(low.compareTo(high) != 1){int flag = 0;BigInteger value = BigInteger.ZERO;mid = high.add(low).divide(BigInteger.valueOf(2));for(int i=0; i<str.length(); i++){value = value.multiply(mid).add(ch2num(str.charAt(i)));if(value.compareTo(decimalValue) == 1){//大于flag = 1;break;}else if(value.compareTo(decimalValue) == 0){//等于flag = 2;break;}}if(flag == 1) high = mid.subtract(BigInteger.ONE);else if(flag == 0) low = mid.add(BigInteger.ONE);else { radix = mid;high = mid.subtract(BigInteger.ONE);}}return radix;}private static BigInteger ch2num(char ch){if(ch >= '0' && ch <= '9'){return BigInteger.valueOf(ch - '0');}else{return BigInteger.valueOf(ch - 'a' + 10);}}private static BigInteger str2Decimal(String str, int radix){BigInteger value = BigInteger.ZERO;for(int i=0; i<str.length(); i++){value = value.multiply(BigInteger.valueOf(radix)).add(ch2num(str.charAt(i)));}return value;}public static void main(String[] args) {String stra, strb;BigInteger a,b;int tag = 0, radix = 0;BigInteger anotherRadix = BigInteger.ZERO;Scanner s = new Scanner(System.in);stra = s.next();strb = s.next();tag = s.nextInt();radix = s.nextInt();if(tag == 1){a = str2Decimal(stra, radix);anotherRadix = findRadix(strb, a);if(anotherRadix.equals(BigInteger.ZERO) == false){System.out.printf("%d", anotherRadix.intValue());}else System.out.printf("Impossible");}else{b = str2Decimal(strb, radix);anotherRadix = findRadix(stra, b);if(anotherRadix.equals(BigInteger.ZERO) == false){System.out.printf("%d", anotherRadix.intValue());}else System.out.printf("Impossible");}}}
- PAT甲级练习1010. Radix (25)
- 【PAT甲级】1010. Radix (25)
- 1010. Radix (25) PAT 甲级
- PAT甲级1010. Radix (25)
- pat甲级1010. Radix (25)
- PAT甲级 1010. Radix (25)
- PAT 甲级 1010.Radix (25)
- PAT 甲级 1010. Radix (25)
- PAT 甲级 1010. Radix
- PAT 甲级 1010. Radix
- [PAT-甲级]1010.Radix
- 1010. Radix (25)-PAT甲级真题(二分法)
- PAT甲级 1010.Radix(25) 题目翻译与答案
- [PAT甲级]1010. Radix (25)(求另一个数的基数)
- PAT-甲级-1010 Radix(25)
- PAT甲级1010:Radix
- 1010. Radix (25)-PAT
- 【PAT】1010. Radix (25)
- 如何优雅地 对深度神经网络 进行训练
- 网站开发进阶(四十五)浅谈XML与HTML的区别
- 最大流EK算法模板
- leecode 解题总结:62. Unique Paths
- redhat虚拟机
- PAT甲级练习1010. Radix (25)
- cocos2dx-js(3.14版本) JS与C++ 互调 JS与JAVA 互调,JS与OC 互调
- leetcode -- 101. Symmetric Tree 【对称树,结构,内容】
- 自定义圆继承button实现多点触控和单点触控
- 深入理解JVM(八)——类加载的时机
- OpenCV画椭圆、实心圆、凹多边形、线段、矩形
- Mybatis中使用占位符#
- pwnable.kr-random-Writeup
- Core Bluetooth