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

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;
ll convert(string n,ll radix)//转换为10进制的函数
{
    ll sum=0;
    int index=0,temp=0;
    for(auto it=n.rbegin(); it!=n.rend(); it++) //通过反向迭代器遍历
    {
        if(*it>=‘0‘&&*it<=‘9‘)
        {
            temp=*it-‘0‘;
        }
        else
        {
            temp=*it-‘a‘+10;
        }
        sum+=temp*pow(radix,index++);
    }
    return sum;
}
ll find_radix(string n,ll num)
{
    ll low,mid,high,t;
    char c=‘0‘;
    for(auto it=n.begin(); it!=n.end(); it++)
    {
        if(*it>c)
        {
            c=*it;
        }
    }
    if(c>=‘0‘&&c<=‘9‘)
    {
        low=c-‘0‘+1;
    }
    else
    {
        low=c-‘a‘+10+1;
    }
    high=max(low,num);
    while(low<=high)
    {
        mid=(low+high)/2;
        t=convert(n,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;
    ll tag,radix,ans;
    cin>>n1>>n2>>tag>>radix;
    if(tag==1)
    {
        ans=find_radix(n2,convert(n1,radix));
    }
    else
    {
        ans=find_radix(n1,convert(n2,radix));
    }
    if(ans!=-1)
    {
        printf("%lld",ans);
    }
    else
    {
        printf("Impossible");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wkfvawl/p/11385164.html

时间: 2024-08-09 02:42:03

PAT 1010 Radix 进制转换+二分法的相关文章

1027. Colors in Mars (20)【进制转换】——PAT (Advanced Level) Practise

题目信息 1027. Colors in Mars (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Re

计算机进制转换

一.计算机只认识0和1,二进制. 二.2进制转换成 8进制 和 16进制,如下图: 二进制 > 八进制 :  研究上图发现,3位最高二进制可以用来表示一位八进制.所以,将二进制分解每3位,不够前面补0,然后每3位转换为10进制,顺序排列即可. 二进制 > 十六进制  :4位最高二进制可以用来表示一位十六进制.所以,将二进制分解每4位,不够前面补0,然后每4位转换为10进制,超过9用字母表示即可.顺序排列即可. 如下: 二进制 > 十进制:   11001001 = 2^7+2^6+2^3

进制进制进制~转换

从刚学计算机器就对进制转换有着莫名的反感,2进制 8进制 10进制 16进制各种转换. 下面就说下逻辑地址转换成物理地址的求法吧 首先,用户输入一个16进制的数字cin>>hex>>logic_add;   hex的意思是告诉计算机输入的数是以16进制方式输入的 这个时候你要是输出cout<<logic_add;  你会发现输出的是把这个16进制的数转换为10进制以后输出的结果 cout<<hext<<logic_add;这样输出的才是16进制.

【String与基本类型之间的转换】以及【进制转换】

1. 基本数据类型---->字符串类型: 方法一:使用连接一个空字符串,例如  基本数据类型+“” : 方法二:静态方法 String.valueOf(),具体有: String.valueOf(boolean b) : 将 boolean 变量 b 转换成字符串 String.valueOf(char c) : 将 char 变量 c 转换成字符串 String.valueOf(char[] data) : 将 char 数组 data 转换成字符串 String.valueOf(char[]

C#——进制转换!

进制转换 No.1 其他进制转十进制——按权展开相加法   即: 由于101.11(10),可写成1X102+0X101+1X100+1X10-1+1X10-2,即(以10为底的乘幂展开式)称按权展开式, 其中:101.11称为“数码” 数制中的(10)称为“基数” 数码中每个位数所具有的值称为“权” 那么其他进制也可写成按权展开式 如:对数字“101.11”的不同进制记数方式为~ 十进制表示为:  101.11(10) 二进制表示为:  101.11(2) 八进制表示为:  101.11 (8

javascript进制转换

一.语法说明 1.parseInt() parseInt:将字符串转换成整数 parseInt(string, radix) string要被解析的字符串. 可选.表示要解析的数字的基数.该值介于 2 ~ 36 之间. 如果省略该参数或其值为 0,则数字将以 10 为基础来解析.如果它以 “0x” 或 “0X” 开头,将以 16 为基数. 如果该参数小于 2 或者大于 36,则 parseInt() 将返回 NaN. 2.toString() toString()方法属于Object对象,Jav

Java 10进制转2、8、16进制转换 / 2、8、16进制转10进制转换

public static void main(String[] args) { int i = 10; System.out.println("***********10进制转换2进制.8进制.16进制************"); System.out.println(Integer.toBinaryString(i)); // 10转换2进制 System.out.println(Integer.toOctalString(i)); // 10转换8进制 System.out.p

java中的进制转换

进制转换 首先是十进制转化为其他进制,如二进制,八进制,十六进制: 它们在java中都有专门的函数可供调用: 如:十进制转化为二进制时调用toBinaryString(); 十进制转化为八进制时调用toOctalString(); 十进制转化为十六进制时调用toHexString(); 代码实现: // 进制转化 public class Main_2 { public static void main(String[] args) { Integer a=new Integer(20); //

进制转换内容总结

今天接触了进制转换的内容,即十进制,二进制,八进制,十六进制之间的相互转换. 一.基础内容 十进制:有十个基数 0,1,2,3,4,5,6,7,8,9 二进制:逢二进一,借一为二. 基数为0,1 八进制:逢八进一,借一为八. 基数为0,1,2,3,4,5,6,7 十六进制:逢十六进一,借一为十六. 基数为0,1,2,3,4,5,6,7,8,9,A(10),B(11),C(12),D(13),E(14),F(15) 二.转换方法 1.十进制与其他进制(二.八.十六) 十进制→ 二进制:十进制数除以