当前位置: 代码迷 >> 综合 >> PAT_A 1010. Radix (25)
  详细解决方案

PAT_A 1010. Radix (25)

热度:19   发布时间:2024-01-11 13:43:15.0

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是从低位开始计算的,在尚未溢出时就已经能比较出结果了,这应该是跟测试点有关。
      not ac
  • 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');elsea10+=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');elsea10+=pow(base,b.length()-i-1)*(b[i]-'a'+10);if(a10>a)return 1;}if(a10<a)return -1;elsereturn 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');elsea10+=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;elsereturn 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;elseout=isSame(a,b,d);if(out==-1)cout<<"Impossible"<<endl;elsecout<<out<<endl;return 0;
}

AC