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 ...
随机推荐
- java基础集合简介Map(三)下
--Map接口简介 今天来看一看map集合,map映射接口,用于存放键值对,<key,value>,通过key来查找value,顾名思义key不能为空,唯一且不重复,不然底层怎么查呢! 可 ...
- 五分钟了解ES6对数值的扩展
文章目录 数值的扩展(ES6) 1. 二进制八进制表示法 2. Number对象 3. Math对象 4. 指数运算符 5. Integer 数据类型 5.1 简介 5.2 运算 数值的扩展(ES6) ...
- var和let部分浅析
ES6中新增了let命令,用于声明变量,但所声明的变量只在let命令的代码块内有效. 举个例子: var a = []; for(var i=0;i<10;i++){ a[i] = functi ...
- PyTorch-网络的创建,预训练模型的加载
本文是PyTorch使用过程中的的一些总结,有以下内容: 构建网络模型的方法 网络层的遍历 各层参数的遍历 模型的保存与加载 从预训练模型为网络参数赋值 主要涉及到以下函数的使用 add_module ...
- 对JDK动态代理的模拟实现
对JDK动态代理的模拟 动态代理在JDK中的实现: IProducer proxyProduec = (IProducer)Proxy.newProxyInstance(producer.getCla ...
- Odoo系统有哪些不同版本?
来源:www.odooyun.com 1. Odoo10.0 vs Odoo11.0 vs 8.0 截至2017年底,最新的Odoo发布版为Odoo 11.0,但功能上有一定精简(去除财务模块,去除工 ...
- Android 框架布局 FrameLayout
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android=" ...
- CODING 敏捷看板全新上线,助力研发管理可视化升级
在服务企业研发团队的过程中,我们发现不少团队碰到了类似的问题: 团队成员声称完成了自己的大部分任务,但团队实际交付的需求却寥寥无几? 由于某些问题导致工序一直处于等待状态,怎么识别和处理这些延迟? 成 ...
- Centos7脚本一键优化
我把优化centos7的脚本分享给大家,建议刚安装完服务器的朋友执行如下优化脚本 [root@test2 yum.repos.d]# cat centos7.sh #!/bin/bash #autho ...
- 第1章:C++泛型技术基础:模板——《C++泛型:STL原理和应用》读书笔记整理
第1章:C++泛型技术基础:模板 1.2 关于模板参数 1.2.1 模板参数类型 类型参数 typename声明的参数都属于类型参数,它的实参必须为系统内置或者用户自定义的数据类型,包括类模板实体 ...