1010 Radix (25)

題目信息

1010 Radix (25)(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 N1 and N2, 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

代碼

#include<iostream>
#include<cctype>
using namespace std;
long long to_decimal(string s,long long radix){
    long long sum=0;
    for(int i=0;i<s.length();i++){
        sum=sum*radix;
        if(isdigit(s[i])) sum+=s[i]-'0';
        if(isalpha(s[i])) sum+=s[i]-'a'+10;
    }
    return sum;
}
long long find_left(string s){
    int max=0;
    for(int i=0;i<s.length();i++){
        if(isdigit(s[i])&&s[i]-'0'>max) max=s[i]-'0';
        if(isalpha(s[i])&&s[i]-'a'+10>max) max=s[i]-'a'+10;
    }
    return max;
}
long long binarySearch(string s,long long left,long long right,long long dec){
    long long mid,s_dec;
    while(left<=right){
        mid=(left+right)/2;
        s_dec=to_decimal(s,mid);
        if(s_dec<0||s_dec>dec) right=mid-1;//若轉(zhuǎn)換后的十進制數(shù)溢出(一定是radix選大了)或較大 
        else if(s_dec<dec) left=mid+1;//選小了 
        if(s_dec==dec) return mid;//返回此時的mid即為最終所找的radix 
    }
    return -1;
}
int main(){
    string n1,n2;
    long long tag,radix,dec1,dec2,flag;
    cin >> n1 >> n2 >> tag >> radix;
    if(tag==1){
        dec1=to_decimal(n1,radix);
        long long left=find_left(n2);
        flag=binarySearch(n2,left+1,dec1+1,dec1);
    }else{
        dec2=to_decimal(n2,radix);
        long long left=find_left(n1);
        flag=binarySearch(n1,left+1,dec2+1,dec2);
    }
    if(flag==-1) cout<<"Impossible";
    else cout<<flag;
    return 0;
} 

測試結(jié)果

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,817評論 0 10
  • 我的PAT系列文章更新重心已移至Github,歡迎來看PAT題解的小伙伴請到Github Pages瀏覽最新內(nèi)容。...
    OliverLew閱讀 568評論 0 0
  • 我們?nèi)绾稳ダ斫庠坡?lián)電器?如何去分享發(fā)展云聯(lián)電器? 相信自己999 已關(guān)注 2018-03-21 22:27 · 字...
    牟姐姐2030閱讀 121評論 0 0
  • 《我要跟自信》這本書,寫的是讓我們要更加自信的許多故事,和讓我們怎么做。 像《草葉集》的作者惠特曼,就是因為自信和...
    皮卡丘萌萌噠閱讀 2,706評論 2 0
  • 2017.10.17晚22:15下班后一如往常先燒一桶水然后…… 靜靜的聽著[書房]靠在床邊,手里拿著本[白夜行]...
    帥啊寧閱讀 312評論 0 0

友情鏈接更多精彩內(nèi)容