zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    spring cglib final @Transactional
    【转】电商架构
    logback发邮件配置
    @Reference不支持继承
    jmap jstack
    dubbo线程池
    C# 爬虫框架实现 流程_爬虫结构/原理
    C# 爬虫框架实现 流程_各个类开发
    C# 爬虫框架实现 概述
    作用域 作用域链 闭包 思想 JS/C++比较
  • 原文地址:https://www.cnblogs.com/Crossea/p/12870366.html
Copyright © 2011-2022 走看看