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;
}
View Code
 
           

 AC代碼:

要用long long,要用二分,否則有兩個數據點會超時(第7和第18個數據點),二分時,left還好,right居然要這么大(否則第7個數據點卡死),而且,算出來的數<0也代表數太大,爆long long ,要ri=mid-1;而且,題目說有多個要挑出最小的那個。

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<map>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int main()
{
    ll rag,radix;
    char a[15];
    char b[15];
    int c[15];
    ll l;
    while(cin>>a>>b>>rag>>radix)
    {
        ll da=0,db;
        if(rag==1)
        {
            ll la=strlen(a);
            ll k=1;
            for(ll 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(ll 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)
        {
            ll lb=strlen(b);
            ll k=1;
            for(ll 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(ll 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';
            }        
        }
        ll ma=0;
        for(ll i=0;i<l;i++)
        {
            if(c[i]>ma)
                ma=c[i];
        }
        ll le=ma+1,ri=10000005;
        bool f=0;
        ll mm=9999999999;
        while(le<=ri)
        {
            ll mid=(ri+le)/2;
            db=0;
            ll k=1;
            for(ll j=l-1;j>=0;j--)
            {
                db+=c[j]*k;
                k=mid*k;
            }
            if(db==da)
            {
                if(mid<mm)//題目有多個要挑出最小的那個
                    mm=mid;
                ri=mid-1;
                f=1;
            }
            else if(db>da||db<0)//算出來的數<0也代表數太大,爆long long ,要ri=mid-1;
            {
                ri=mid-1;
            }
            else if(db<da)
            {
                le=mid+1;
            }

        }
        if(f==0)
            cout<<"Impossible"<<endl;
        else
        {
            cout<<mm<<endl;
        }
    }
    return 0;
}

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM