[PAT] Grade 1010 Radix (25 minutes) (binary)

Meaning of the questions: Enter two numbers may contain lowercase letters, 1 or 2, hex size. The third number is the number represented by the first hexadecimal number of the fourth, the second request when the number is equal to a first binary number size, output Impossible impossible, the third number is 2 for the second number is the hexadecimal number of the fourth, first find the second number is equal to a hexadecimal number size, the output is impossible impossible. The number of digits at most ten.

trick: hex may be large, so the calculation process may burst long long, do not worry, you can deal with as a negative number.

Code:

#include<bits/stdc++.h>
using namespace std;
string s1,s2;
long long t,rad;
int main(){
cin>>s1>>s2>>t>>rad;
if(t==2)
swap(s1,s2);
long long num1=0,num2=0;
int len1=s1.size(),len2=s2.size();
for(int i=0;i<len1;++i){
num1*=rad;
if(s1[i]>='0'&&s1[i]<='9')
num1+=s1[i]-'0';
else
num1+=s1[i]-'a'+10;
}
int mx=0;
for(int i=0;i<len2;++i)
if(s2[i]>='0'&&s2[i]<='9')
mx=max(mx,s2[i]-'0');
else
mx=max(mx,s2[i]-'a'+10);
long long l=mx+1;
long long r=max(l,num1);
long long mid=0;
while(l<=r){
mid=(l+r)>>1;
String X = S2;
Long Long tmp = 0;
for (int I = 0; I <LEN2; ++ I) {
tmp * = MID;
IF (X [I]> = '0' && X [I] <= ' . 9 ')
tmp + = X [I] -' 0 ';
the else
tmp + = X [I] -' A '+ 10;
}
IF (tmp> num1 || tmp <0) // sequential write anti be wrong
r = mid -1;
the else IF (tmp <num1) // anti-sequential write will be wrong because the overflow tmp less than 0, then, must be smaller than num1
L = MID +. 1;
the else {
COUT << MID;
return 0;
}
}
COUT << " Impossible ";
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11228282.html