[PAT]1010. Radix (25)@Java解题报告
来源:互联网 发布:残疾人婚恋软件 编辑:程序博客网 时间:2024/06/16 21:18
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
题意理解:
求是否有一个进制使得在此进制下,未知进制的数和另一个数在十进制下相等。存在则输出满足条件最小的进制,不存在则Impossible
网上大部分是用c++写的,参考了别人的代码。
package go.jacob.day828;import java.math.BigInteger;import java.util.Scanner;/** * 1010. Radix (25) * @author Jacob * 关键点:用BigInteger代替Long */public class Demo2 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String n1 = sc.next(), n2 = sc.next();int tag = sc.nextInt(), radix = sc.nextInt();// 方便计算,把n1当做已知进制,n2未知if (tag == 2) {String tmp = n1;n1 = n2;n2 = tmp;}//将n1转成数字BigInteger num1 = str2Num(n1, radix);//找出num1中的最大数字BigInteger low=findBiggest(n2);BigInteger minRadix=BigInteger.ZERO;BigInteger left=low.add(BigInteger.ONE);BigInteger right=num1.max(BigInteger.valueOf(50)).add(BigInteger.ONE);while(left.compareTo(right)!=1){//二分法,mid为left+(right-left)/2;BigInteger的写法与一般写法不同BigInteger mid=left.add(right).divide(BigInteger.valueOf(2));BigInteger tmpValue=BigInteger.ZERO;//设置标志,记录tmpValue与num1的大小关系int flag=-1;for(int i=0;i<n2.length();i++){tmpValue=tmpValue.multiply(mid).add(char2Num(n2.charAt(i)));if(tmpValue.compareTo(num1)==1){flag=1;break;}else if(tmpValue.compareTo(num1)==0){flag=0;break;}}//根据flag判断当前mid与midValue大小关系if(flag==1)right=mid.subtract(BigInteger.ONE);else if(flag==-1)left=mid.add(BigInteger.ONE);else{minRadix=mid;right=mid.subtract(BigInteger.ONE);}}if(minRadix.equals(BigInteger.ZERO))System.out.println("Impossible");elseSystem.out.println(minRadix);sc.close();}/* * 找出字符串中字符表示的最大数字 */private static BigInteger findBiggest(String str) {BigInteger max=BigInteger.ZERO;for(int i=0;i<str.length();i++){if(max.compareTo(char2Num(str.charAt(i)))==-1)max=char2Num(str.charAt(i));}return max;}/* * 将输入字符串转化为BigInteger */private static BigInteger str2Num(String str, int radix) {BigInteger value=BigInteger.ZERO;for(int i=0;i<str.length();i++){value=value.multiply(BigInteger.valueOf(radix)).add(char2Num(str.charAt(i)));}return value;}/* * 字符转BigInteger */private static BigInteger char2Num(Character c) {BigInteger value;if (c >= '0' && c <= '9')value = BigInteger.valueOf(c - '0');elsevalue = BigInteger.valueOf(c - 'a' + 10);return value;}}
- [PAT]1010. Radix (25)@Java解题报告
- 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 (25)
- PAT 1010. Radix (25)
- 【PAT】1010. Radix (25)
- pat 1010. Radix (25)
- PAT-1010. Radix (25)
- PAT 1010. Radix(25)
- PAT A 1010. Radix (25)
- ZJU-PAT 1010. Radix (25)
- 【PAT甲级】1010. Radix (25)
- PAT A 1010. Radix (25)
- 1010. Radix (25) PAT 甲级
- 我的第一个博客
- 域服务篇 04. 组 ❀ Windows Server 2016
- code-push-server的部署(docker)
- sqlserver 日期加减问题
- 运维知道
- [PAT]1010. Radix (25)@Java解题报告
- [js高手之路]从原型链开始图解继承到组合继承的产生
- B
- A+B Problem II
- 1 个方法提升 3 倍执行力
- 用shell脚本实现监控程序自动重启
- 45. Jump Game II
- 三分搜索总结
- 删除桌面快捷键的箭头