zoukankan      html  css  js  c++  java
  • PAT 1010 Radix

    1010. Radix (25)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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

    给你两个数,问你,一个数用什么进制转换之后可以等于另一个数,由于进制可以很大,可以二分求解

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <math.h>
    #include <stdio.h>
    #include <string>
    #include <vector>
    #include <strstream>
    #include <map>
    
    using namespace std;
    typedef long long int LL;
    string str1,str2;
    int tag;LL radix;
    LL fun(string str,LL radix)
    {
        LL num1=0;
      int len1=str.length();
        for(int i=0;i<len1;i++)
        {
        num1*=radix;
            if(str[i]<='9'&&str[i]>='0')
                num1+=(LL)(str[i]-'0');
            else if(str[i]<='z'&&str[i]>='a')
                num1+=(LL)(str[i]-'a'+10);
        if(num1<0)
          return -1;
        }
        return num1;
    }
    int main()
    {
    
        cin>>str1>>str2;
        scanf("%d%lld",&tag,&radix);
       
        LL num1=0;LL num2=0;
        if(tag==2)
            swap(str1,str2);
      int len1=str1.length();
        int len2=str2.length();
        LL ans;
        num1=fun(str1,radix);
        int minn=1;
        for(int i=0;i<len2;i++)
        {
            if(str2[i]<='9'&&str2[i]>='0')
                minn=max(minn,str2[i]-'0');
            else if(str2[i]<='z'&&str2[i]>='a')
                minn=max(minn,str2[i]-'a'+10);
        }
      LL l=minn+1;
      LL r=num1+1;
      bool flag=false;
      while(l<=r)
      {
        LL mid=(l+r)/2;
        num2=fun(str2,mid);
        if(num2==-1||num2>num1)
          r=mid-1;
        else if(num2==num1)
        {
          ans=mid;
          flag=true;
          break;
        }
        else
          l=mid+1;
      }
      if(!flag)
            printf("Impossible
    ");
        else
            printf("%lld
    ",ans);
        return 0;
    
    }


  • 相关阅读:
    java学习 接口与继承11 默认方法
    java学习 接口与继承10 内部类
    java学习 接口与继承9 抽象类
    java学习 接口与继承8 final
    理解管理信息系统
    vue中的错误日志
    vue中的ref属性
    2.有24颗外观完全一样的小球,其中有一个是空心的,现在只有一个天平,最少称几次能找出这个特殊的球?
    1.有888瓶编了号码的水及10只健康的小白鼠,其中一瓶水有毒,小白鼠饮用毒水一天后会死,最少需要几天可以找到哪瓶水有毒?
    SQL题1两表联查
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228619.html
Copyright © 2011-2022 走看看