ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PAT Advanced 1010 Radix

2021-01-24 17:02:55  阅读:153  来源: 互联网

标签:num radix PAT int long Radix N1 N2 1010


题目

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

理解与算法

题目不是特别复杂,难的是测试用例范围,测试用例可能非常非常大,超出int的范围,甚至会超出long long int的范围,变成负数,所以编程的时候要考虑到这点。

还要确定进制的上限与下限,最小进制应该比这个数中最大的那位数字大1,最大进制应该不超过前一个已经确定好进制的数。考虑到数的范围,我们全部用long long int来实现。

char it = *max_element(N2.begin(), N2.end());
long long low = (isdigit(it) ? it - '0' : it - 'a' + 10) + 1;
long long high = max(num, low);

写这道题目的时候要想想如何优化,如果每一个进制都试一遍那肯定会超时,考虑到进制越大,数的值肯定会变大,而我们已经确定好数的进制的范围,相当于进制与数值是一个递增函数,所以我们就能用二分法来加快搜索速度!

while (low <= high) {
    long long mid = (low + high) / 2;
    long long t = to_decimal(N2, mid);
    if (t < 0 || t > num) high = mid - 1;
    else if (t == num) return mid;
    else low = mid + 1;
}

代码与实现

#include <iostream>
#include <algorithm>
#include <cctype>
#include <cmath>

using namespace std;

long long int to_decimal(string num, long long int radix) {
    long long int sum = 0, index = 0, tmp;
    for (long long int i = num.length() - 1; i >= 0; --i) {
        tmp = isalpha(num[i]) ? (num[i] - 'a' + 10) : (num[i] - '0');
        sum += tmp * pow(radix, index++);
    }
    return sum;
}

long long find_radix(long long int num, string N2) {
    char it = *max_element(N2.begin(), N2.end());
    long long low = (isdigit(it) ? it - '0' : it - 'a' + 10) + 1;
    long long high = max(num, low);
    while (low <= high) {
        long long mid = (low + high) / 2;
        long long t = to_decimal(N2, mid);
        if (t < 0 || t > num) high = mid - 1;
        else if (t == num) return mid;
        else low = mid + 1;
    }
    return -1;
}

int main() {
    string N1, N2, tmp;
    long long int flag, radix;
    cin >> N1 >> N2 >> flag >> radix;
    // 交换N1与N2
    if (flag == 2)
        swap(N1, N2);

    long long int result = find_radix(to_decimal(N1, radix), N2);
    if (result > 0)
        cout << result;
    else
        cout << "Impossible";
    return 0;
}

标签:num,radix,PAT,int,long,Radix,N1,N2,1010
来源: https://www.cnblogs.com/tanknee/p/14321386.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有