• PAT 1010 Radix


    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

    需要注意的点如下:

    1.待计算的radix,输入的N1,N2,这三个数字计算出来可能很大,所以必须要用long long 数据类型(8个字节)
    2.如果待计算的N只有一位digit,那么就可能有无数个radix满足条件,根据题意,需要选最小的,要做分支处理
    3.确定calculateRadix的bottom和top,可以顺序循环并计算出N的十进制值并进行比较,但是循环太多次的话会超时,所以要改用二分法去循环
    4.准确的写出二分查找
    5.写题的过程中,尽可能抽取独立的方法,使代码readable,方便理清思路


    
    #include "iostream"
    #include "string"
    using namespace std;
    
    //单个字符数字[0-9a-z]转数字
    int getCharValue(char c);
    //计算radix并打印
    void calculateRadixAndPrint(string inputParamString, string calculateParamString, long long inputRadix);
    //计算radix
    long long getRadix(long long bottomRadix, string calculateParamString, long long sureValue);
    //比较指定的radix下,digit字符串的值是否与 targetValue相等,-1小于,0等于 1大于
    int compare(string calculateParamString, long long tryRadix, long long targetValue);
    
    int main(){
    
        string n1;
        string n2;
        int tag;
        long long radix;
    
        cin >> n1 >> n2 >> tag >> radix;
        if (tag == 1) {
    
            calculateRadixAndPrint(n1, n2, radix);
    
        } else {
            calculateRadixAndPrint(n2, n1, radix);
        }
    
    }
    
    void calculateRadixAndPrint(string inputParamString, string calculateParamString, long long inputRadix) {
        long long inputValue = 0;
    
        string impossible = "Impossible";
        long long m = 1;
        for (int i = inputParamString.size() - 1; i >= 0; --i) {
            inputValue += (getCharValue(inputParamString[i]) * m);
            m *= inputRadix;
        }
    
        long long bottomRadix = 0;
    
        for (int i = 0; i < calculateParamString.size(); ++i) {
            int num = getCharValue(calculateParamString[i]);
            if (bottomRadix < num) {
                bottomRadix = num;
            }
        }
    
        long long radixResult = getRadix(bottomRadix, calculateParamString, inputValue);
        if (radixResult == -1) {
            cout << impossible;
        } else {
            cout << radixResult;
        }
    }
    
    
    long long getRadix(long long bottomRadix, string calculateParamString, long long sureValue){
        //区分情况
        if (calculateParamString.size() == 1) {
            //只有一位
            long long value = getCharValue(calculateParamString[0]);
            if (value == sureValue) {
                return value + 1;
            } else {
                return -1;
            }
        } else {
            //有多位的情况
            long long topRadix = sureValue + 1;
    
            //binary search
            while (bottomRadix <= topRadix) {
                long long middle = (bottomRadix + topRadix) / 2;
    
                int compareResult = compare(calculateParamString, middle, sureValue);
                if (compareResult == 0) {
                    return middle;
                } else {
                    if (compareResult < 0) {
                        bottomRadix = middle + 1;
                    } else if (compareResult > 0) {
                        topRadix = middle - 1;
                    }
                }
            }
    
            return -1;
        }
    
    }
    
    
    int getCharValue(char c) {
        if (c >= 'a') {
            return (int)c -(int)'a' + 10;
        } else {
            return (int)c - (int)'0';
        }
    }
    
    
    int compare(string calculateParamString, long long tryRadix, long long targetValue){
        long long paramvalue = 0;
        long long m = 1;
        for (int i = calculateParamString.size() - 1; i >= 0 ; --i) {
            paramvalue += getCharValue(calculateParamString[i]) * m;
            m *= tryRadix;
            if (paramvalue > targetValue || paramvalue < 0) {
                return 1;
            }
        }
        if (paramvalue > targetValue) {
            return 1;
        }else if (paramvalue == targetValue) {
            return 0;
        } else {
            return -1;
        }
    }
    
    
    
    
    
  • 相关阅读:
    GO异常 | runnerw.exe: CreateProcess failed with error 21
    tab切换效果
    第一次码前端页面的经历
    开博第一天
    软件架构参考模板-软件架构设计学习第三天(原创)
    架构设计基础-软件架构设计学习第二天(非原创)
    IT架构师介绍-软件架构设计学习第一天(非原创)
    Android常见面试题学习第二天(原创)
    Android常见面试题学习第一天(原创)
    Netty简单介绍(非原创)
  • 原文地址:https://www.cnblogs.com/IC1101/p/14153627.html
Copyright © 2020-2023  润新知