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
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
long long str2num(string s, long long radix){
long long num = ;
for (int i = ; i < s.length(); i++){
if (s[i] >= '' && s[i] <= '')num = num + (s[i] - '')*pow(radix, s.length() - i - );
else if (s[i] >= 'a' && s[i] <= 'z')num = num + (s[i] - 'a' + )*pow(radix, s.length() - i - );
}
return num;
}
long long min_radix(string s){
long long max_r = ;
for (int i = ; i < s.length(); i++){
long long tmp;
if (s[i] >= '' && s[i] <= '')tmp = s[i] - '';
else if (s[i] >= 'a' && s[i] <= 'z')tmp = s[i] - 'a' + ;
if (tmp>max_r)max_r = tmp;
}
return max_r+;
}
int main(){
string n1, n2;
long long num1 = , num2 = ;
long long tag, radix;
long long min_r = , res = , le, ri, mid;
cin >> n1 >> n2 >> tag >> radix;
if (tag == ){
num1 = str2num(n1, radix);
min_r = min_radix(n2); }
else{
num1 = str2num(n2, radix);
min_r = min_radix(n1);
}
le = min_r;
ri = max(le,num1);
while (le <= ri){
mid = (le + ri) / ;
num2 = str2num(tag==?n2:n1, mid);
if (num2< || num2 > num1)ri = mid - ;
else if (num2 < num1)le = mid + ;
else{
res = mid;
break;
}
} if (res != )printf("%d", res);
else printf("Impossible");
system("pause");
}

注意点:第一:两个输入题目保证不超过10位,但没说几进制,所以可能会很大,不过好在测试数据都没有超过long long。

第二:针对这种两个换一换的情况最好写函数,简洁明了,还可以直接用swap函数(algorithm头文件里)

第三:由于没有明确上界,需要自己判断

第四:范围太大,逐个遍历会超时,要用二分法

第五:由于指定数不超过long long,在特定radix下得到的值溢出long long(小于0),说明太大

第六:基数的下限通过遍历字符串得到,下限肯定比最大的数大1

第七:上界为ri = max(le,num1);,这里一直想不通,其实是这样的,如果已知数比最小基数要小,用最小基数也还是可能算出已知数来的,比如已知10进制的8,未知数为8,9进制就可以了。

PAT A1010 Radix (25 分)——进制转换,二分法的更多相关文章

  1. PAT 1010 Radix 进制转换+二分法

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  2. A1010 Radix (25 分)

    一.技术总结 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型).函数内部知识,使用函数迭代器,即auto it = n.rbeg ...

  3. PAT 1010 Radix (25分) radix取值无限制,二分法提高效率

    题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...

  4. PAT-1015 Reversible Primes (20 分) 进制转换+质数

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

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

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

  6. Bugku-CTF加密篇之进制转换(二进制、八进制、十进制、十六进制,你能分的清吗?)

    进制转换 二进制.八进制.十进制.十六进制,你能分的清吗?

  7. C++中进制转换问题

    一直在刷题的时候,都会遇到一个坑,就是进制转换的问题.而每一次都傻乎乎的自己去实现一个.所以算是对以前的坑的一个总结. itoa 函数 itoa是广泛应用的非标准C语言和C++语言扩展函数.由于它不是 ...

  8. PAT A1010.Radix 二分法

    PAT A1010.Radix 链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 算法 ...

  9. Java基础笔记(3) 进制与进制转换

    ---恢复内容开始--- 进制 在一般生活中,我们一直在应用的十进制,就是逢十进一,而今天我们要接触的是,计算机编程常用的进制!首先我们要知道,计算机内部运算采用的是二进制,也就是逢二进制! 1.什么 ...

  10. 颜色转换、随机、16进制转换、HSV

    颜色转换.随机.16进制转换.HSV: /** * * *-----------------------------------------* * | *** 颜色转换.随机.16进制转换.HSV * ...

随机推荐

  1. 读 《CSharp Coding Guidelines》有感

    目录 基本原则 类设计指南 属性成员设计指南 其他设计指南 可维护性指南 命名指南 性能指南 框架指南 文档指南 布局指南 相关链接 C# 编程指南 前不久在 Github 上看见了一位大牛创建一个仓 ...

  2. MEF 插件式开发之 WPF 初体验

    MEF 在 WPF 中的简单应用 MEF 的开发模式主要适用于插件化的业务场景中,C/S 和 B/S 中都有相应的使用场景,其中包括但不限于 ASP.NET MVC .ASP WebForms.WPF ...

  3. API接口规范V1.0——制定好规范,才好合作开发

    返回码规范: 统一六位 000000 表示成功! 参数相关返回码预留100000-199999:系统相关返回码预留200000-299999:数据中心310000-319999后续项目以此类推,后续根 ...

  4. design mode(php)

    前段时间看了下设计模式 参考,以及head first设计模式,简要如下 ## OO原则 * 封装变化* 多用组合,少用继承* 针对接口编程,不针对实现编程* 为交互对象之间的松耦合设计而努力* 开闭 ...

  5. APP(通信)接口定义

  6. Python 练习:三级菜单选择城市

    info = { 'GuangDong':{ 'GuangZhou': ['TianHe', 'HaiZhu'], 'MaoMing': ['MaoNan', 'DianBai']}, 'ShanDo ...

  7. 微信小程序地图报错——ret is not defined

    刚刚在使用微信的map做地图时候 发现如下报错: 后来找了一会发现错误为经纬度写反了导致经纬度超出了范围 正确取值范围: latitude   纬度  浮点数,范围 -90 ~ 90 longitud ...

  8. 【读书笔记】iOS-iCloud文件备份

    iOS应用在运行时经常要创建一些文件,不过这些文件要如何存放呢?有没有什么要求呢? 由于手机资源空间有限而且考虑到Apple推出的iCloud,我们确实要对创建出的文件按照作用的不同,分出几种类别出来 ...

  9. 即时消息服务框架(iMSF)应用实例之分布式事务三阶段提交协议的实现

    一,分布式事务简介 在当前互联网,大数据和人工智能的热潮中,传统企业也受到这一潮流的冲击,纷纷响应国家“互联网+”的战略号召,企业开始将越来越多的应用从公司内网迁移到云端和移动端,或者将之前孤立的IT ...

  10. Cordova/Ionic开发的Android APP启用Chrome Inspect调试的方法

    Cordova/Ionic开发的Android APP,需要启用WebView的调试模式,才可以在Chrome浏览器中输入chrome://Inspect,然后使用大家熟悉的开发者工具进行调试.不启用 ...