码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-08-13 20:43:49      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:names   int   ica   ring   ecif   二分   ssi   space   spec   

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,之后再改)

标签:names   int   ica   ring   ecif   二分   ssi   space   spec   

原文地址:https://www.cnblogs.com/caiyishuai/p/9470446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!