zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    获取spring源码并导入到eclipse
    Android的EditText设置可编辑与不可编辑的方法
    漫谈设计模式笔记:模板模式
    jfreechar中文乱码设置主题样式解决
    FrameLayout布局下让图片居中的方法
    java典型模块实例1:英文,数字,中文混合的验证码
    学习Lucene笔记一:创建索引
    How to Display a PDF File in a HTML Web Page
    NET数据类型及字节数
    2012年1月编程语言排行榜
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/9470446.html
Copyright © 2011-2022 走看看