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

题目意思:给你两个数N1和N2,并给出来其中一个数的进制作为基数radix,求另一个数的进制是多少时二者相等。第三个数tag如果是1表示radix是N1的进制,求N1的进制;如果是2表示radix是N2的进制,求N1的进制。

解题思路:需要N1和N2各自进制下表示的数相同,需要一个标准,可以都转换成十进制,需要一个函数convert,第一个参数是数值,第二个参数是进制,将这个数值转换为十进制。

数字表示使用[0-9,a-z],所以刚开始想的是另外一个进制的基数范围是1~36,这其实是不对的,基数范围是可以大于这个范围的,最大的情况其实是需要表示的该数的十进制,逐个遍历不是很理想,最佳方式是使用二分法。同时这道题还有一个坑点,如果使用当前进制转换得到的数值比另一个大,说明进制选大了,同时还要注意有可能产生溢出,也就是使用当前进制转换得到的数值小于0,原因也是进制选大了。

#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=;
int index=,temp=;
for(auto it=n.rbegin(); it!=n.rend(); it++) //通过反向迭代器遍历
{
if(*it>=''&&*it<='')
{
temp=*it-'';
}
else
{
temp=*it-'a'+;
}
sum+=temp*pow(radix,index++);
}
return sum;
}
ll find_radix(string n,ll num)
{
ll low,mid,high,t;
char c='';
for(auto it=n.begin(); it!=n.end(); it++)
{
if(*it>c)
{
c=*it;
}
}
if(c>=''&&c<='')
{
low=c-''+;
}
else
{
low=c-'a'++;
}
high=max(low,num);
while(low<=high)
{
mid=(low+high)/;
t=convert(n,mid);
if(t<||t>num)
{
high=mid-;
}
else if(t==num)
{
return mid;
}
else
{
low=mid+;
}
}
return -;
} int main()
{
string n1,n2;
ll tag,radix,ans;
cin>>n1>>n2>>tag>>radix;
if(tag==)
{
ans=find_radix(n2,convert(n1,radix));
}
else
{
ans=find_radix(n1,convert(n2,radix));
}
if(ans!=-)
{
printf("%lld",ans);
}
else
{
printf("Impossible");
}
return ;
}

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

  1. PAT Radix[二分][进制转换][难]

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  2. PAT甲级 进制转换题_C++题解

    进制转换题 PAT (Advanced Level) Practice 进制转换题 目录 <算法笔记> 重点摘要 1015 Reversible Primes (20) 1019 Gene ...

  3. JS 进制转换

    十进制转换成其他进制 objectname.toString([radix])   objectname 必选项.要得到字符串表示的对象. radix 可选项.指定将数字值转换为字符串时的进制. 例如 ...

  4. [No000071]C# 进制转换(二进制、十六进制、十进制互转)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. C++ 中数串互转、进制转换的类

    /******************************************************************** created: 2014/03/16 22:56 file ...

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

    1. 基本数据类型---->字符串类型: 方法一:使用连接一个空字符串,例如  基本数据类型+“” : 方法二:静态方法 String.valueOf(),具体有: String.valueOf ...

  7. java 13-4 Integer和String、int之间的转换,进制转换

    1.int类型和String类型的相互转换 A.int -- String 推荐用: public static String valueOf(int i) 返回 int 参数的字符串表示形式. B. ...

  8. Swift3.0 进制转换

    Swift3.0 进制转换 模块可以直接使用,写的不是很好,欢迎来喷 // Data -> HexStrings func dataToHexStringArrayWithData(data: ...

  9. Delphi进制转换(二进制/十进制/十六进制)

    http://www.cnblogs.com/ywangzi/archive/2012/12/12/2815219.html   Delphi进制转换(二进制/十进制/十六进制)  2009-11-2 ...

随机推荐

  1. flutter学习之环境配置

    1.Android SDK通常目录: 用户->用户名->AppData->Local=>Android->Sdk 2.不知道的情况下,打开Android Studio,然 ...

  2. 理解django的框架为何能够火起来

    理解django的框架为何能够火起来 https://www.yiibai.com/django/django_basics.html https://code.ziqiangxuetang.com/ ...

  3. 如何下载Vimeo视频

    MediaHuman YouTube Downloader是应用在Mac上的一款非常优秀的YouTube视频下载工具,YouTube Downloader破解版将帮助你快速完成视频下载,而不会挂断.您 ...

  4. 日志介绍与rsyslogd服务管理与配置

    一.日志简介 1.日志相关服务介绍 在 CentOS 6.x 中日志服务使用 rsyslogd 服务,rsyslogd 具有以下特点: 基于 TCP 网络协议传输日志信息 更安全的网络传输方式 有日志 ...

  5. C#线程学习笔记四:线程同步

    本笔记摘抄自:https://www.cnblogs.com/zhili/archive/2012/07/21/ThreadsSynchronous.html,记录一下学习过程以备后续查用.     ...

  6. java对象的实例化过程

    简单类对象的实例化过程 1.在方法区加载类: 2.在栈内存申请空间,声明变量P: 3.在堆内存中开辟空间,分配对象地址: 4.在对象空间中,对对象的属性进行默认初始化,类成员变量显示初始化: 5.构造 ...

  7. 【高可用架构】用Nginx实现负载均衡(三)

    前言 在上一篇,已经用Envoy工具统一发布了Deploy项目代码.本篇我们来看看如何用nginx实现负载均衡 负载均衡器IP 192.168.10.11 [高可用架构]系列链接:待部署的架构介绍 演 ...

  8. oopday02(面向对象-构造方法&静态static)

    面向对象之封装 01_面向对象(构造方法Constructor概述和格式) * A:构造方法概述和作用 * 给对象的数据(属性)进行初始化 * B:构造方法格式特点 * a:方法名与类名相同(大小也要 ...

  9. CentOS 7 离线环境安装nginx时报错:./configure: error: C compiler cc is not found

    先说解决方法: 在nginx目录下,查看objs/autoconf.err文件,该文件记录了具体的错误信息 vi objs/autoconf.err 一般就是缺少一些文件,因为我的gcc.g++也是离 ...

  10. Linux - CentOS 7 安装 .Net Core 运行环境

    阿里云的CentOS 7.7 64位,所需要的环境:MySql 5.7,.Net Core 2.2 ,Nginx 我这里用的 Xshell 工具,首先用root进入系统 版本信息 打开终端输入命令: ...