zoukankan      html  css  js  c++  java
  • 1010 Radix (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

    Submit:

    #include <iostream>
    #include <algorithm>
    #include <string>
    using namespace std;
    //目标:把n1的radix进制的转为10进制,再根据二分查找,尝试寻找n2对应的mid进制转为10进制比较相等则输出mid(最终所求)
    //进制转换,把radix进制的s转为10进制
    long long convert(string s, int radix) {//转化过程可能溢出 ,所以用long long 类型
        long long i, ans=0;//10进制结果
        for (i=0;i<s.length();i++) {
            char t = s[i];
            ans = isdigit(t) ? ans*radix+t-'0' : ans*radix + t-'a'+10;
        }
        return ans;
    }
    //二分查找
    long long binSearch(string s, long long num) {
        char it = *max_element(s.begin(), s.end());//求出字符串中的最大字符
        long long low = (isdigit(it) ? it -'0' : it -'a' + 10)+1;//把最大字符转为数字或字母 再加1防止进位 
        long long high = max(low, num);///求最大值和已知的10进制数的最大值 如 8 8 1 10
        while (low <= high) {
            long long mid = (low+high)/2;
            long long t = convert(s,mid);//把第二个字符串以mid进制转为10进制进行比较
            if (t<0 || t>num) high = mid-1;
            else if(t == num) return mid;//相等表示是 mid进制
            else low = mid +1;//t<num 从后半部分取
        }
        return -1;
    }
    int main(){
        long long tag,radix,result;
        string n1,n2;
        cin >> n1 >> n2 >> tag >> radix ;
        long long temp = convert(n1,radix);
        result = tag == 1 ? binSearch(n2,convert(n1,radix)) : binSearch(n1,convert(n2,radix));//tag=1,二分查找n2,和radix进制的n1转为十进制
        if (result != -1) printf("%lld",result);
        else printf("Impossible");
        return 0;
    }

    参考:

    柳婼-https://blog.csdn.net/liuchuo/article/details/54561626

    昵称五个字-https://blog.csdn.net/a617976080/article/details/89676670

  • 相关阅读:
    kettle 3:java调用transformation
    SEO网站优化笔记
    图案设计的素材
    TweenLite 使用详解(译文)
    flv文件修复文件头
    一个拼图素材
    外螺旋矩阵排列
    20160226.CCPP体系详解(0036天)
    tcp三次握手与四次分手
    docker 应用数据的管理之bind mounts
  • 原文地址:https://www.cnblogs.com/cgy-home/p/15127962.html
Copyright © 2011-2022 走看看