PAT-1010解题报告

题目是这样的,原封不动复制过来:

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

读完题目第一感觉是这题难度不大,按照常规思路来:

1)将tag标记的数(简记为Nt)转换成十进制表示;

2)计算另外一个数(简记为No)最小的可能的进制min_radix,只需遍历这个数的各个位,得到其中最大的值Max,那么min_radix就是Max+1;

3)从Max+1开始,将No转换成十进制,直到No >= Nt;

4)如果No==Nt,输出此时对应的No的进制,否则,输出Impossible。

其中有一个特殊情况可以在一开始处理:

No为一位数,此时直接判断No是否与Nt的十进制表示相等,是则输出No的min_radix,否则,输出Impossible;


实现的时候,发现有一个case超时(第几个记不住了,其他case都通过)。反复调试,还是超时。后来百度了下,得知,测试用例的radix可能非常大(long long int级的),因此,直接采用线性遍历的方法必然在遇到大case的时候会超时。于是改用二分法,用于上述算法中第3)步的遍历过程。代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int findradix(char * n);   //returns the radix of n
int char2int(char c);
long long int convert2ten(char * n, long long int radix);   //converts n to 10 radix
long long int pow(long long int a, int b);

int main(){
	int tag;
	long long int radix, radix_no_tag, i, c1, c2, up, down;
	char N1[11], N2[11];
	memset(N1, 0, 11);
	memset(N2, 0, 11);
	scanf("%s %s %d %lld", N1, N2, &tag, &radix);
	if(tag == 1){
		radix_no_tag = findradix(N2);
		c1 = convert2ten(N1, radix);
		up = c1;
		down = radix_no_tag;
		if(N2[1] == NULL && N2[0] <= '9' && N2[0] >= '0'){
			if(char2int(N2[0]) != c1){
				printf("Impossible");
				return 0;
			}
			printf("%lld", radix_no_tag);
			return 0;
		}
		if(strcmp(N2, "10") == 0){
			printf("%lld", c1);
			return 0;
		}
		while(down <= up){
			i = (down + up)/2;
			c2 = convert2ten(N2, i);
			//if(c2 < 0){
			//	up--;
			//	continue;
			//}
			if(c2 == c1){
				printf("%lld", i);
				return 0;
			}
			if(c2 > c1)up = i - 1;
			else
				down = i + 1;
		}
		if(down > up){
			printf("Impossible");
			return 0;
		}
	}
	if(tag == 2){
		radix_no_tag = findradix(N1);
		c2 = convert2ten(N2, radix);
		up = c2;
		down = radix_no_tag;
		if(N1[1] == NULL && N1[0] <= '9' && N1[0] >= '0'){
			if(char2int(N1[0]) != c2){
				printf("Impossible");
				return 0;
			}
			printf("%lld", radix_no_tag);
			return 0;
		}
		if(strcmp(N1, "10") == 0){
			printf("%lld", c2);
			return 0;
		}
		while(down <= up){
			i = (down + up)/2;
			c1 = convert2ten(N1, i);
			//if(c1 < 0){
			//	up--;
			//	continue;
			//}
			if(c2 == c1){
				printf("%lld", i);
				return 0;
			}
			if(c1 > c2)up = i - 1;
			else
				down = i + 1;
		}
		if(down > up){
			printf("Impossible");
			return 0;
		}
	}
	return 0;
}

int findradix(char * n){
	int i = 0, radix = -1, temp;
	while(n[i] != NULL){
		if((temp = char2int(n[i])) > radix)radix = temp;
		i++;
	}
	return radix + 1;
}

int char2int(char c){
	if(c <= '9' && c >= '0')return (c - '0');
	return (c + 10 - 'a');
}

long long int convert2ten(char * n, long long int radix){
	int i = 0, j;
	long long int res = 0;
	if(n[1] != NULL && n[0] == '0'){
		//1 radix
		for(i = 0; n[i] != NULL; i++);
		return i;
	}
	for(; n[i] != NULL; i++);
	for(j = 0; j < i; j++){
		res += char2int(n[j]) * pow(radix, i - j - 1);
	}
	return res;
}

long long int pow(long long int a, int b){
	int i = 0;
	long long int res = 1;
	if(b == 0)return 1;
	if(a == 0)return 0;
	for(; i < b; i++)res *= a;
	return res;
}

代码中有两块被注释掉的代码段,这两段代码的功能是在溢出的时候调整二分法的上界,如果没有这段代码,会有若干case过不掉,原因是二分法的上界可能过大,导致便利过程中的中间结果溢出,得出错误的输出。那么二分法的上界是什么呢?假设最开始的算法中第1)步得到的Nt的十进制表示为Nt10, 那么这就是二分法的上界,至于为什么,这里不多解释(可以参考 http://blog.csdn.net/whosemario/article/details/8734508)。

做这题最大的一个收获是了解到了一个处理溢出的手段,思想很简单:每次得到中间结果之后判断是否有溢出,如果有,则对上界(或下界)进行微调,再重复计算。


今后编程或者实际项目开发中,要更加重视程序的效率,尽量减少时间和空间的开销,尤其注意数据的上下界,避免由于溢出可能导致的程序错误。由此也想到了由于整型溢出进而引发缓冲溢出的常见程序缺陷。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值