PAT甲级练习1010. Radix (25)

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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值