PAT 甲级 1010 Radix (25)(25 分)(听说要用二分,我没AC,之后再改)

Posted caiyishuai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 甲级 1010 Radix (25)(25 分)(听说要用二分,我没AC,之后再改)相关的知识,希望对你有一定的参考价值。

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

23分代码
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<map>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
    int rag,radix;
    char a[15];
    char b[15];
    int c[15];
    int l;
    while(cin>>a>>b>>rag>>radix)
    {
        int da=0,db;
        if(rag==1)
        {
            int la=strlen(a);
            int k=1;
            for(int i=la-1;i>=0;i--)
            {
                if(a[i]>=a&&a[i]<=z)
                {
                    da+=(a[i]-a+10)*k;
                }
                else
                    da+=(a[i]-0)*k;
                k=k*radix;
            }
            l=strlen(b);
            for(int i=l-1;i>=0;i--)
            {
                if(b[i]>=a&&b[i]<=z)
                {
                    c[i]=b[i]-a+10;
                }
                else
                    c[i]=b[i]-0;
            }    
        }

        if(rag==2)
        {
            int lb=strlen(b);
            int k=1;
            for(int i=lb-1;i>=0;i--)
            {
                if(b[i]>=a&&b[i]<=z)
                {
                    da+=(b[i]-a+10)*k;
                }
                else
                    da+=(b[i]-0)*k;
                k=k*radix;
            }
            l=strlen(a);
            for(int i=l-1;i>=0;i--)
            {
                if(a[i]>=a&&a[i]<=z)
                {
                    c[i]=a[i]-a+10;
                }
                else
                    c[i]=a[i]-0;
            }        
        }
        int ma=0;
        for(int i=0;i<l;i++)
        {
            if(c[i]>ma)
                ma=c[i];
        }
        bool f=0;
        for(int i=ma+1;i<=1000005;i++)
        {
            db=0;
            int k=1;
            for(int j=l-1;j>=0;j--)
            {
                db+=c[j]*k;
                k=k*i;
            }
            if(db==da)
            {
                f=1;
                cout<<i<<endl;
                break;
            }
        }
        if(f==0)
            cout<<"Impossible"<<endl;
    }
    return 0;
}

 








以上是关于PAT 甲级 1010 Radix (25)(25 分)(听说要用二分,我没AC,之后再改)的主要内容,如果未能解决你的问题,请参考以下文章

PAT甲级1010

PAT甲级 1010 Radix

PAT甲级 1010 Radix

PAT 甲级 1010 Radix

PAT 1010 Radix (25分) radix取值无限制,二分法提高效率

PAT 甲级测试题目 -- 1010 Radix