• 1010 Radix (25分) PAT


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


    #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;
    }
    
  • 相关阅读:
    课后作业-阅读任务-阅读提问-4
    团队-象棋游戏-开发文档
    团队-象棋游戏-模块测试过程
    团队编程项目作业3-模块开发过程
    团队-象棋游戏-项目进度
    团队编程总结
    团队编程项目作业,维护
    个人编程总结2
    阅读笔记4
    课后提问4
  • 原文地址:https://www.cnblogs.com/Crossea/p/12870366.html
Copyright © 2020-2023  润新知