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
题目意思:给你两个数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 进制转换+二分法的更多相关文章
- PAT Radix[二分][进制转换][难]
1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...
- PAT甲级 进制转换题_C++题解
进制转换题 PAT (Advanced Level) Practice 进制转换题 目录 <算法笔记> 重点摘要 1015 Reversible Primes (20) 1019 Gene ...
- JS 进制转换
十进制转换成其他进制 objectname.toString([radix]) objectname 必选项.要得到字符串表示的对象. radix 可选项.指定将数字值转换为字符串时的进制. 例如 ...
- [No000071]C# 进制转换(二进制、十六进制、十进制互转)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C++ 中数串互转、进制转换的类
/******************************************************************** created: 2014/03/16 22:56 file ...
- 【String与基本类型之间的转换】以及【进制转换】
1. 基本数据类型---->字符串类型: 方法一:使用连接一个空字符串,例如 基本数据类型+“” : 方法二:静态方法 String.valueOf(),具体有: String.valueOf ...
- java 13-4 Integer和String、int之间的转换,进制转换
1.int类型和String类型的相互转换 A.int -- String 推荐用: public static String valueOf(int i) 返回 int 参数的字符串表示形式. B. ...
- Swift3.0 进制转换
Swift3.0 进制转换 模块可以直接使用,写的不是很好,欢迎来喷 // Data -> HexStrings func dataToHexStringArrayWithData(data: ...
- Delphi进制转换(二进制/十进制/十六进制)
http://www.cnblogs.com/ywangzi/archive/2012/12/12/2815219.html Delphi进制转换(二进制/十进制/十六进制) 2009-11-2 ...
随机推荐
- 【CHRIS RICHARDSON 微服务系列】微服务架构中的进程间通信-3
编者的话 |本文来自 Nginx 官方博客,是微服务系列文章的第三篇,在第一篇文章中介绍了微服务架构模式,与单体模式进行了比较,并且讨论了使用微服务架构的优缺点.第二篇描述了采用微服务架构的应用客户端 ...
- mac-安装java、安装maven
首先检查自己的设备是否已经安装了jdk,在cmd终端输入,如已安装出现对应的版本信息,未安装弹出提示窗,官方网址:http://www.oracle.com/technetwork/java/java ...
- Provider模式应用demo
参考ObjectPool对象池设计原理还原一个简易的Provider模式. using System; using System.Dynamic; using System.Reflection.Me ...
- SAP中的数据库表索引
数据库表中的索引可以加快查询的速度.索引是数据库表字段的有序副本.附加的字段包含指向真实数据库表行的指针.排序可以使访问表行的速度变快,例如,可以使用二分搜索.数据库表至少有一个主索引,由它的key字 ...
- STM32的Keil找不到想要flash的解决方法
STM32的Keil找不到想要flash的解决方法:https://blog.csdn.net/qq_38376586/article/details/79582020
- .NET Core 使用HMAC算法
一. HMAC 简介 通过哈希算法,我们可以验证一段数据是否有效,方法就是对比该数据的哈希值,例如,判断用户口令是否正确,我们用保存在数据库中的password_md5对比计算md5(password ...
- WebShell代码分析溯源(八)
WebShell代码分析溯源(八) 一.一句话变形马样本 <?php $e=$_REQUEST['e'];$arr= array('test', $_REQUEST['POST']);uasor ...
- Python高级特性——切片(Slice)
摘录廖雪峰网站 定义一个list: L = ['haha','xixi','hehe','heihei','gaga'] 取其前三个元素: >>> L[0],L[1],L[2] (' ...
- tesseract-OCR + pytesseract安装
1. tesseract-OCR下载安装 地址:https://digi.bib.uni-mannheim.de/tesseract/ 选择一个版本下载,下载完成点击**.exe进行安装,若无其他需求 ...
- NLTK实现文本切分
之前已经了解了使用nltk库,将文本作为参数传入相应函数进行切分的方法,下面看看使用正则表达式如何来进行文本切分. 1. 使用正则表达式切分 1.1 通过RegexpTokenizer 进行切分.先导 ...