您的位置:首页 > 其它

PAT_A 1010. Radix (25)

2016-11-27 18:09 423 查看

1010. Radix (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

分析

做这道题,恶心的不能在恶心了,是在是到坑题,但它又不是在数据结构和算法上多难的那种。先说我遇到的坑:

我用循环做,一个答案错误,一个超时。答案错误那个是因为直接求各种进制转化后的结果,应该是溢出了。



改成二分查找,错了好久。 原来是comp1比较函数那溢出了,但comp2没有发生溢出,comp1是从高位开始计算,comp2是从低位开始计算的,在尚未溢出时就已经能比较出结果了,这应该是跟测试点有关。



code

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
//转化成10进制数
long long eq10(string a,long long base)
{
long long a10=0;
for(long long i=0;i<a.length();i++)
{
if(a[i]>='0'&&a[i]<='9')
a10+=pow(base,a.length()-i-1)*(a[i]-'0');
else
a10+=pow(base,a.length()-i-1)*(a[i]-'a'+10);
}
return a10;
}
//从高位开始计算,提前比较的方式并没完全起到作用,发生溢出了
int comp1(long long a,string b,long long base)
{
long long a10=0;
for(long long i=0;i<b.length();i++)
{
if(b[i]>='0'&&b[i]<='9')
a10+=pow(base,b.length()-i-1)*(b[i]-'0');
else
a10+=pow(base,b.length()-i-1)*(b[i]-'a'+10);
if(a10>a)
return 1;
}
if(a10<a)
return -1;
else
return 0;
}
//从地位开始计算,溢出前,就比较出结果,因此没有溢出
int comp2(long long a,string b,long long base)
{
long long a10=0;
for(long long i=b.length()-1;i>=0;i--)
{
if(b[i]>='0'&&b[i]<='9')
a10+=pow(base,b.length()-i-1)*(b[i]-'0');
else
a10+=pow(base,b.length()-i-1)*(b[i]-'a'+10);
if(a10>a)
return 1;
}
if(a10>a)
return 1;
if(a10<a)
return -1;
else
return 0;
}

long long isSame(string a,string b,long long base)
{
//判断b的最低进制
long long max=0;
long long num=0;
for(long long i=0;i<b.length();i++)
{
if(b[i]>='0'&&b[i]<='9')
{
num=(b[i]-'0');
}else
{
num=(b[i]-'a'+10);//把10给忘了
}
if(max<num)
max=num;
}

long long a10=eq10(a,base);
long long b10=0;
//确定范围
long long _max=a10+1;
long long _min=max+1;
if(a10<max)
{
_max=max+1;
_min=a10+1;
}
long long m=_min;
int cc=0;
//二分法
while(_min<=_max)
{
//cc=comp1(a10,b,m);
cc=comp1(a10,b,m);
if(cc==1)
_max=m-1;
else if(cc==-1)
_min=m+1;
else if(cc==0)
return m;
m=(_min+_max)/2;
}
return -1;
}
int main()
{
string a,b;
long long c,d;
cin>>a>>b>>c>>d;
long long out=-1;
if(c==2)
{
string tmp=a;
a=b;
b=tmp;
}
if(a==b)//如果不添加这个,或有一个错误,应该考虑去掉这
out=d;
else
out=isSame(a,b,d);
if(out==-1)
cout<<"Impossible"<<endl;
else
cout<<out<<endl;
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  坑题