PAT(甲) 1010 Radix (25)(详解)

本文介绍了一种使用二分查找法解决特定进制转换问题的方法。具体来说,对于给定的一对正整数,任务是找出其中一个数的进制,使得这两个数在不同进制下相等。文章提供了完整的C++代码实现,并详细解释了二分查找的边界条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.


  • 输入格式
    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.

  • 输出格式
    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.


解题方法:
这道题的根本在于找到另一个数是否能有一个进制可以转换为前者。那么自然而然需要用到遍历的方法,一般的方法如果线性搜索必然是会超时的(注意:进制不仅仅只有2、4、8、16),因此可以考虑用二分法进行搜索。


易错点:
1. 二分法搜索下限为该N进制数最大字符所代表的数字+1,否则第一个无法通过
2. 二分法搜索上限为已确定的数的数值
3. 再用pow函数需要注意里面参数类型,不清楚的可以参考我的另一篇博文


程序:

#include <algorithm>
#include <string.h>
#include <cctype>
#include <cmath>
#include <iostream>
using namespace std;

char FindMaxElement(char ch[])
{   /* 寻找字符数组中最大的元素 */
    return *max_element(ch, ch+strlen(ch));
}

int charToint(char c)
{   /* 把字符转为数字 */
    return isdigit(c) ? c - '0' : c - 'a' + 10;
}

long long transfer(char ch[], long long radix)
{   /* 转换成十进制 */
    long long sum = 0;
    for (int i = strlen(ch)-1; i >= 0; i--)
        sum += charToint(ch[i]) * pow(radix, strlen(ch)-1-i);
    return sum;
}

long long check(char ch[], long long num)
{   /* 采用二分法进行寻找 */
    long long low = charToint(FindMaxElement(ch))+1, mid; /* 这里下界是ch中最大的字符数值+1 */
    long long high = num > low ? num : low;
    while (low <= high)
    {
        mid = (low+high) / 2;
        long long aws = transfer(ch, mid);
        if (aws < 0 || aws > num)
            high = mid - 1;
        else if (aws == num)
            return mid;
        else
            low = mid + 1;
    }
    return -1;
}

int main(int argc, char const *argv[])
{
    char N1[11], N2[11];
    int tag;
    long long r, radix;
    cin >> N1 >> N2 >> tag >> radix;
    r = tag == 1 ? check(N2, transfer(N1, radix)) : check(N1, transfer(N2, radix));
    if (r == -1)
        cout << "Impossible";
    else
        cout << r;
    return 0;
}

如果对您有帮助,帮忙点个小拇指呗~

### 关于1010 Radix测试点的技术含义及用途 对于特定标记为“1010 Radix”的测试点,在计算机科学领域内通常涉及的是基数(Radix)的概念,这主要关联到数据表示形式以及算法设计中的数制转换。然而,“1010”作为一个二进制字符串可以直接被解释为十进制下的数值10;当提到“1010 Radix”,可能是指基于不同进制间相互转化的一种测试案例。 #### 基数概念解析 在信息技术里,基数指的是某个计数系统的底数或基础。例如: - **二进制** (Binary): 使用01两个符号,其基数为2; - **八进制** (Octal): 使用0至7八个符号,基数为8; - **十进制** (Decimal): 日常生活中常用的数字系统,使用0到9十个符号,基数为10; - **十六进制** (Hexadecimal): 计算机编程中常用,除了0-9外还加入了A-F六个字母作为额外的记号,因此基数为16。 针对“1010 Radix”这一表述,如果将其视为一个特殊的测试场景,则可能是为了验证程序能否正确处理来自多种进制的数据输入,并确保这些数据能够在不同的基数之间准确无误地互相转换[^1]。 #### 实际应用场景举例 考虑如下Python代码片段用于展示如何实现简单的多进制间的转换功能: ```python def convert_base(number_str, from_radix=10, to_radix=10): """Convert a string representing an integer in one base to another.""" decimal_value = int(number_str, from_radix) result = '' while decimal_value > 0: remainder = decimal_value % to_radix if remainder >= 10: # Convert numbers above 9 into corresponding letters A-Z. char = chr(ord('A') + remainder - 10) else: char = str(remainder) result = char + result decimal_value //= to_radix return '0' if not result else result print(convert_base('1010', 2, 10)) # Output should be "10" ``` 此函数`convert_base()`可以接收三个参数:待转换的原始数值串、源基数(`from_radix`)以及目标基数(`to_radix`)。通过调用内置的`int()`函数先将给定的字符串按照指定的原生基数转成内部统一使用的十进制整型值,之后再依据目的基数逐步构建新的表达方式直至完成整个过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值