PAT甲级 1010 Radix 好多坑

题目链接

题目

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 N
​1
​​ and N
​2
​​ , 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

思路

根据tag,先把已知进制的数转换为10进制,然后再用二分搜索,对未知进制的数进行转换。思路参考柳神:https://www.liuchuo.net/archives/2458

代码

#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
long long getRes(string s,long long t){
	long long res=0;
	int num=0,index=0;
	for(int i=s.size()-1;i>=0;i--){
		if(s[i]>='0' && s[i]<='9')num=s[i]-'0';
		else num=s[i]-'a'+10;
		res+=num*pow(t,index++);
	}
	return res;
}
long long find_radix(string s,long long num){
	char it=*max_element(s.begin(),s.end());
	long long low=(isdigit(it)?it-'0':it-'a'+10)+1;
	long long high=max(num,low);
	while(low<=high){
		long long mid=(low+high)/2;
		long long temp=getRes(s,mid);
		if(temp<0 || temp>num)high=mid-1;
		else if(temp==num)return mid;
		else low=mid+1; 
	} 
	return -1; 
	
}

int main(){
	string n1,n2; 	
	long long radix,P=0,tag,res;
	cin>>n1>>n2>>tag>>radix;
	if(tag==1){
		P=getRes(n1,radix);
		res=find_radix(n2,P);	
	}
	else{
		P=getRes(n2,radix);
		res=find_radix(n1,P);
	}
	if(res!=-1)cout<<res;
	else cout<<"Impossible"; 
	
	return 0; 
} 

排坑记录以及柳神代码知识点记录

坑1:未知进制的范围不是1-35,千万不能被题目中的0-35迷惑了。(自己没看题解写代码时因为没注意到这个一直在16分左右徘徊
坑2:未知进制的最小值 = 未知进制的数中出现的最大的字符+1
对应代码:

	char it=*max_element(s.begin(),s.end());
	long long low=(isdigit(it)?it-'0':it-'a'+10)+1;

例如 N2=ab (这是未知进制的数),那么所求的进制的最小值就是 b=11+1=12
这是因为这个数原本就至少11进制了(出现了b)那么所求进制显然不可能小于11。
【这里为什么要加一我也没搞懂,如果不加一的话有几个点是过不了的】

坑3:未知进制的最大值=max(坑2的最小值,要转换成的那个10进制数)
对应代码

long long high=max(num,low);

当进制大于所要转换的10进制,转换不可能成立。比如 N2=1(待转换),N1对应的十进制为6。不妨设待转换数的进制等于7,那么N2=1 (7进制) = 7 (10进制)>N1=6 (10进制)。

知识点

1.max_element的使用 ,参考:链接
2.判断long long溢出的方法:如果两个正数之和等于负数,或者两个负数之和之和等于正数,那么就是溢出。参考链接
对应代码

if(temp<0 || temp>num)high=mid-1; //这里temp小于0就是溢出 【此处不是100%肯定】

3.<math.h>中的pow函数,参数不是double也能运行

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值