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


  • 相关阅读:
    一个表对应另一个表中多个主键的查询方法(把一个表当成两个表用)
    可以切换数据库的SqlHelper
    win7安装后的用户选择
    如何删除 Windows.old 文件夹
    Windows Server 2008磁盘清理工具
    sqlserver express版PRIMARY 大小不能超过4G
    一交换机,一光猫、一路由器组internet网的方法
    公司部门职责清晰
    IIS下载EXE(拾遗)
    win2008 IIS 7.0中WebDAV
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228619.html
Copyright © 2011-2022 走看看