首页 > 其他 > 详细

1010 Radix (25分) PAT

时间:2020-05-11 19:08:12      阅读:45      评论:0      收藏:0      [点我收藏+]

技术分享图片

先求出第一个值,然后二分第二个数的基数就可以


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
long long convert(string a, long long radix){
	long long ret=0;
	for(auto it=a.begin(); it!=a.end(); it++){
		int i=isdigit(*it)?*it-‘0‘:*it-‘a‘+10;
		ret=ret*radix+i;
	}
	return ret;
}
char find_max(string a){
	char temp=‘0‘;
	for(int i=0; i<a.length(); i++)
		temp=max(temp,a[i]);
	return temp;
}
long long binarySearch(string num,long long target){
	char c=find_max(num);
	long long low=isdigit(c)?c-‘0‘:c-‘a‘;
	low +=1;
	long long high=max(low,target+1);
	long long mid;
	long long res=-1;
	while(low<=high){
		mid=low+((high-low)>>1);
		long long t=convert(num,mid);
		if(t==target){
			res=mid;
			high=mid-1;
		}
		else if(t<0||t>target)
			high=mid-1;
		else
			low=mid+1;
	}
	return res;
}
int main(int argc, char const *argv[])
{
	string a,b;
	long long tag,radix;
	cin>>a>>b>>tag>>radix;
	if(tag==1){
		long long target=convert(a,radix);
		long long res=binarySearch(b,target);
		if(res==-1LL)
			cout<<"Impossible";
		else
			cout<<res;
	}else{
		long long target=convert(b,radix);
		long long res=binarySearch(a,target);
		if(res==-1LL)
			cout<<"Impossible";
		else
			cout<<res;
	}
	return 0;
}

1010 Radix (25分) PAT

原文:https://www.cnblogs.com/Crossea/p/12870366.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!