PAT甲级练习1010. Radix (25)

来源:互联网 发布:微信一键传图软件 编辑:程序博客网 时间:2024/05/24 06:06

1010. Radix (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
这题好烦啊。。。

这题参考了别人的思路,参见 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");}}}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 5岁半宝宝有狐臭怎么办? 4岁宝宝脾气倔怎么办 小孩子摔到后脑勺吐了怎么办 学生报告册丢了怎么办 错过宝宝语言敏感期怎么办 父亲骂我很难听怎么办 90后父母催婚怎么办 和隔壁邻居吵架了怎么办 教师上课期间孩子出问题责任怎么办 孩子一考试妈妈就焦虑怎么办 妈妈爱发脾气孩子也发脾气怎么办 孩子对家人有抵触情绪怎么办 孩子因为没耐心哭闹怎么办 宝宝哭闹真是没耐心怎么办 对自己孩子没耐心怎么办 跳了把腰伤了怎么办 爸爸对妈妈家暴怎么办 逆反心强23岁怎么办 觉得自己心理有问题怎么办 小孩心理脆弱过于敏感怎么办? 父母老是骂打我怎么办 爸妈打架闹离婚怎么办 夫妻离婚后孩子户口怎么办 离婚后孩子户口怎么办落户 50岁父母要离婚怎么办 父母离婚小孩没有出生证该怎么办 离婚。父母一直在劝说该怎么办 老婆跟人跑了怎么办啊 父母50了要离婚怎么办 父母吵架了我该怎么办 父母看孩子总是吵架怎么办 20岁父母离婚我怎么办 碰到没素质的人怎么办 父母抛弃了我该怎么办 父母说家里没钱困难怎么办 遇到素质低的老婆怎么办 孩子考试心里素质差怎么办 5岁儿童脾气不好怎么办 有一个素质差的父母怎么办 孩子对什么都无所谓怎么办 孩子在学习上无所谓怎么办