ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PAT甲级 1010 Radix 好多坑

2021-01-21 23:33:22  阅读:167  来源: 互联网

标签:10 radix PAT 进制 tag long Radix N2 1010


题目链接

题目

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也能运行

标签:10,radix,PAT,进制,tag,long,Radix,N2,1010
来源: https://blog.csdn.net/niudaidai233/article/details/112974370

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有