zoukankan      html  css  js  c++  java
  • 1010. Radix (25) pat

    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
    

    题意很好理解,给出两个数,给出其中一个的radix,求是否存在适合的radix让两个数相等,
    最初还在想radix的范围,就想肯定不能是1,最小是2,题中说最大为z(35),应该是2-36,肯定不对,没好好考虑,
    要确定范围,下边界肯定不能从2开始,应该找出最大的数字,对他加一,才是下边界,因为题目说A digit is less than its radix,
    然后上边界应该是下边界和给出radix的那个数的当中最大的那个,这个比较好理解,上边界肯定不能比下边界小,如果给出的N比下边界大,那么分两种情况,
    未知radix的数如果是一位数,那么就不用管上边界了,假如有合适的肯定是下边界,如果不是单位数,肯定大于等于10,以N为进制的10正好等于N,所以上边界不能超过N了。
    再就是如果排着尝试,必定会超时,可以用二分法,人家没说数的范围,封顶是64位长整型,计算的过程中容易溢出,变为负数要做特殊处理。

    代码:
    #include <iostream>
    using namespace std;
    int main()
    {
        string a,b;
        long long tag,radix;
        long long l,r;
        int flag = 0;
        cin>>a>>b>>tag>>radix;
        long long aa = 0,bb = 0;
        if(tag == 2)swap(a,b);///这样做可以少写点代码,不用分情况了
    
        for(int i = 0;i < a.size();i ++)///先计算已知radix的数
        {
            if(a[i] >= '0' && a[i] <= '9')
            {
                aa = aa * radix + a[i] - '0';
            }
            else if(a[i] >= 'a' && a[i] <= 'z')
            {
                aa = aa * radix + a[i] - 'a' + 10;
            }
        }for(int i = 0;i < b.size();i ++)
        {
            if(b[i] >= 'a' && b[i] <= 'z' && b[i] - 'a' + 10 >= l)l = b[i] - 'a' + 11;///下边界这里直接加一了,本来是加十,所以加十一
            else if(b[i] >= '0' && b[i] <= '9' && b[i] - '0' >= l)l = b[i] - '0' + 1;
        }
        r = aa > l?aa:l;///确定上边界
        long long mid = (l + r) / 2;
        while(l <= r)
        {
            bb = 0;
            for(int j = 0;j < b.size();j ++)
            {
                if(b[j] >= '0' && b[j] <= '9')
                {
                    bb = bb * mid + b[j] - '0';
                }
                else if(b[j] >= 'a' && b[j] <= 'z')
                {
                    bb = bb * mid + b[j] - 'a' + 10;
                }
                if(bb < 0 || bb > aa)break;///溢出的特殊处理
            }
            if(aa == bb)
            {
                cout<<mid;
                flag = 1;
                break;
            }
            if(bb < 0 ||bb > aa)r = mid - 1;///溢出的特殊处理else l = mid + 1;
            mid = (l + r) / 2;
        }
        if(flag == 0)cout<<"Impossible";
    }
  • 相关阅读:
    RDLC报表开发技巧备忘
    C#3.0"扩展方法"简记
    将activity转化成view
    取消logcat输出
    低栏的项目切换
    android edittext 限制文本框输入的长度和提示信息
    android如何调用dotnet编写的webservice
    iphone 式退出
    几种tabhost的总结(1)
    切换浏览示意图
  • 原文地址:https://www.cnblogs.com/8023spz/p/7661915.html
Copyright © 2011-2022 走看看