1010 Radix(25 分)

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 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
//本题关键是确定进制的上限,可以有很大很大,故而设为long long类型
//进制转换和二分查找法是本题的解决方法

#include<stdio.h>
#include<iostream>
#include<string>

using namespace std;

//字符转化为数字
int charToInt(char ch)
{
    int rs= 0;
    if(ch >= ‘0‘ && ch <= ‘9‘)
    {
        rs = ch - ‘0‘;
    }

    if(ch >= ‘a‘ && ch <= ‘z‘)
    {
        rs = ch - ‘a‘ + 10;
    }

    return rs;
}

//化为十进制数
long long changeToDecimal(string s,long long radix)
{
    long long result = 0;
    for(int i = 0;i < s.length();i++)
    {
        result = result*radix + charToInt(s[i]);
    }

    return result;
}

//比较n1与n2在十进制下的大小,此函数可避免运行超时
int compare(long long n1, string n2, long long radix)
{
    long long sum = 0;
    for (int i = 0; i < n2.length();i++)
    {
        sum = sum * radix + charToInt(n2[i]);
        if (sum > n1) //重点,避免运行超时
        {
            return 1;
        }
    }
    if (sum > n1)// n1 < n2
    {
        return 1;
    }
    else if (sum < n1) // n1 > n2
    {
        return -1;
    }
    else //相等
    {
        return 0;
    }

}

//二分查找,十进制数与未知进制数相等时的进制
long long Binary_Search(long long n,string p)
{
    //step1:未知进制数的进制下界(每个位置数字的最大值+1)
    //      进制上界(数d1和数b最大符号代表的数的最大值加上1,n>最小进制时,则为n+1;否则为最小进制,可以设为low+1)

    int max = 0;
    for(int i = 0;i < p.length();i++)//从末位开始
    {
        if(max < charToInt(p[i])) //不为空
        {
            max = charToInt(p[i]);
        }
    }

    long long low = (long long)max + 1;//记录最小的进制;
    long long high;//记录最大进制;
    if(n > low)
    {
        high = n + 1;
    }
    else
    {
        high = low + 1;
    }
    long long mid;//中间数

    //step2:二分查找
    while(low < high)
    {
        mid = (low + high)/2;
        if(compare(n,p,mid) == 0)//(n == changeToDecimal(p,mid))
        {
            return mid;
        }
        else if(compare(n,p,mid) == 1)//(n < changeToDecimal(p,mid))
        {
            high = mid;   //while中是low <= high时,为mid-1;此处是low < high;但按照书中一模一样的写法,只能得24分
        }
        else
        {
            low = mid;    //while中是low <= high时,为mid+1;此处是low < high
        }
    }

    return -1;
}

int main()
{
    string N1,N2;
    int tag;
    long long radix;
    cin >> N1;
    cin >> N2;
    cin >> tag;
    cin >> radix;

    //全部化为10进制比较
    long long d1,d2;//N1和N2化为十进制的值
    if(tag == 1)
    {
        d1 = changeToDecimal(N1,radix);
        if(Binary_Search(d1,N2) == -1)
        {
            cout << "Impossible" << endl;
        }
        else
        {
            cout << Binary_Search(d1,N2) << endl;
        }
    }

    if(tag == 2)
    {
        d2 = changeToDecimal(N2,radix);
        if(Binary_Search(d2,N1) == -1)
        {
            cout << "Impossible" << endl;
        }
        else
        {
            cout << Binary_Search(d2,N1) << endl;
        }
    }

    return 0;
}

原文地址:https://www.cnblogs.com/meiqin970126/p/9607252.html

时间: 2024-10-28 15:51:14

1010 Radix(25 分)的相关文章

PAT 1010. Radix (25)

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 i

1010. Radix (25)——PAT (Advanced Level) Practise

题目信息: 1010. Radix (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 b

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 rad

1010. Radix (25)

简直是哭着做出来的,起码提交了22遍.创纪录了... 首先不能像上次那样枚举进制数,那样输入987654321 10 1 10 的话十分钟都算不完,题中只有一个超时很照顾人了... 考虑近似计算,利用y=a*x^n+b 算出n,进制数如果有,也不会超过n,因此考虑由n向下寻找. 注意,进制数不能小于数中最大的数字. 如果将两个数字作为字符串输入,注意排除字符串前面的0. 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN,

pat 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 N?1?? and N?2??, your task is to find the radi

A1010 Radix (25 分)

一.技术总结 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型).函数内部知识,使用函数迭代器,即auto it = n.rbegin(),这里it作用就是指针一样,装的是地址,了解函数begin()是返回字符串str的一个元素地址和函数end()是返回字符串最后一个元素还要往后一位,而rbegin()和rend(),加了r后刚刚反过来了,rbegin()指向str的最后一个元素,rend()指向第一个元素还要往前一位. 第二

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any

1036 Boys vs Girls (25 分)

1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students. Input Specification: Each input file contains one test case. Each case contai

1016 Phone Bills (25 分)

1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecti