PAT A1010 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 N​1 and N​2 , 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

题意:

输入四个数字,分别是 N1,N2,tag,radix.
如果tag为1的话,就找到一个数x,使N1的radix进制和N2的x进制相同。
如果tag为2的话,就找到一个数x,使N2的radix进制和N1的x进制相同。
如果能找到的话,就输出这个数x,如果找不到,就输出Impossible。

注意点:

1.题目输入的数字中,0至9代表数字至9。小写字母a至z分别代表10至36;
2.本题目的重点是进制转换,很多人在求进制转换的时候可能会用到cmath头文件里面的pow函数。需要注意的是,pow函数返回的是一个浮点数,如果将其赋值给一个整数的话,就会直接进行取整操作。例如,24.999999999可能就会变成24.造成误差,遇到这种情况,需要使用round函数来四舍五入。
3.题目中要求找到x进制,由题分析可知,x数字最少要比要求该进制的数字中的最大数字还大。例如,如果一个数字是19,那么它最少是十进制的数字,不可能出现7进制的19.但是我们也不知道x最大可能为多少,所以我们要从最小值到另一个数字之间的所有数字都计算进去。这样的情况下,时间复杂度会超级超级超级的大!我们必须使用二分查找,否则有一个测试点会超时。

代码:

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

long long int convert(string a,int radix)//把a转换成radix进制
{
    long long int res = 0;
    int times = 0;
    for(int i = a.size()-1;i >= 0;i --)
    {
        if(a[i] <= '9' && a[i] >= '0')
        {
            res += round((a[i] - '0')*pow(radix,times));
        }
        else
        {
            res += round((a[i] - 'a' + 10)*pow(radix,times));
        }
        times ++;
    }
    return res;
}
int main()
{
    string s1,s2;
    int tag;
    int radix;
    cin >> s1 >> s2 >> tag >> radix;
    if(tag == 2){
        string x;
        x = s2;
        s2 = s1;
        s1 = x;
    }
    long long int re1;
    long long int re2;
    re1 = convert(s1,radix);
    int flag = 0;
    int maxs = 0;
    for(int i = 0;i < s2.size();i ++)//找到s2中的最大的数字
    {
        int now;
        if(s2[i] <= '9' && s2[i] >= '0')
        {
            now = s2[i] - '0';
        }
        else if(s2[i] <= 'z' && s2[i] >= 'a')
        {
            now = s2[i] - 'z' + 10;
        }
        if(now > maxs) maxs = now;
    }

    long long int low = maxs + 1;
    long long int high = max(low,re1) + 1;
    long long int mid;
    while(low <= high)
    {
        mid = (high + low) / 2;
        re2 = convert(s2,mid);
        if(re2 > re1 || re2 < 0)
        {
            high = mid - 1;
        }
        else if(re2 == re1)
        {
            low = mid;
            flag = 1;
            break;
        }
        else
        {
            low = mid + 1;
        }
    }
    if(flag)
    {
        cout << low;
    }
    else
    {
        cout << "Impossible";
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值