PAT A1010 Radix (25 分)——进制转换,二分法
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 <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 分)——进制转换,二分法的更多相关文章
- PAT 1010 Radix 进制转换+二分法
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- A1010 Radix (25 分)
一.技术总结 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型).函数内部知识,使用函数迭代器,即auto it = n.rbeg ...
- PAT 1010 Radix (25分) radix取值无限制,二分法提高效率
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- PAT-1015 Reversible Primes (20 分) 进制转换+质数
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- PAT甲级 进制转换题_C++题解
进制转换题 PAT (Advanced Level) Practice 进制转换题 目录 <算法笔记> 重点摘要 1015 Reversible Primes (20) 1019 Gene ...
- Bugku-CTF加密篇之进制转换(二进制、八进制、十进制、十六进制,你能分的清吗?)
进制转换 二进制.八进制.十进制.十六进制,你能分的清吗?
- C++中进制转换问题
一直在刷题的时候,都会遇到一个坑,就是进制转换的问题.而每一次都傻乎乎的自己去实现一个.所以算是对以前的坑的一个总结. itoa 函数 itoa是广泛应用的非标准C语言和C++语言扩展函数.由于它不是 ...
- PAT A1010.Radix 二分法
PAT A1010.Radix 链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 算法 ...
- Java基础笔记(3) 进制与进制转换
---恢复内容开始--- 进制 在一般生活中,我们一直在应用的十进制,就是逢十进一,而今天我们要接触的是,计算机编程常用的进制!首先我们要知道,计算机内部运算采用的是二进制,也就是逢二进制! 1.什么 ...
- 颜色转换、随机、16进制转换、HSV
颜色转换.随机.16进制转换.HSV: /** * * *-----------------------------------------* * | *** 颜色转换.随机.16进制转换.HSV * ...
随机推荐
- 【github&&git】3、git图像化界面GUI的使用
GIT学习笔记 一. 基础内容 1.git是一个版本控制软件,与svn类似,特点是分布式管理,不需要中间总的服务器,可以增加很多分支. 2.windows下的git叫msysgit,下载 ...
- js之返回网页顶部
目标效果:浏览网页过程中,滑动滚轮,显示返回顶部按钮,点击返回顶部后,返回网页顶端. 代码如下: <!DOCTYPE html> <html lang="en"& ...
- JS校验身份证号的合法性
前端表单中有身份证号的校验,下边是用JS来校验身份证号的合法性. 中国居民身份证号码编码规则 第一.二位表示省(自治区.直辖市.特别行政区). 第三.四位表示市(地级市.自治州.盟及国家直辖市所属市辖 ...
- 英文技术Podcast推荐 - 英语技术一起学
Podcast(播客)是现在比较流行的音.视频RSS订阅媒体.跟大家分享一下我所关注的一些不错的英文技术podcast,大家感兴趣可以订阅,在关注国外最前沿的技术资讯的同时更加锻炼英文听力(有很多需要 ...
- loadrunner 运行场景-Controller及Load Generators宿主主机优化
运行场景-Controller及Load Generators宿主主机优化 by:授客 QQ:1033553122 1. Load Generator宿主主机优化设置 win7以下:进入“控制面板” ...
- 程序员简单打造一个灵活智能的自动化运维系统C#实例程序
你是一个程序员,被派去管理公司500台计算机.这些机器可能需要执行一些自动化任务,一台台手动操作会把你累死.重复性的工作还是交给电脑处理,怎么解决这个问题呢?一个自动化的运维系统是必须的.自己实现的好 ...
- python爬虫之Beautifulsoup学习笔记
相关内容: 什么是beautifulsoup bs4的使用 导入模块 选择使用解析器 使用标签名查找 使用find\find_all查找 使用select查找 首发时间:2018-03-02 00:1 ...
- Scala学习笔记2 (带着问题学习, 逐渐扩展。理解吃透scala.)
问题: 把 文本字符串"[1, 2, 3, 4, 5]" 转换成一个数组. 答案: val x = "[1, 2, 3, 4, 5]" val y =x sli ...
- List基础操作
/** * List基础操作 * Created by zhen on 2018/11/14. */ object ListDemo { def main(args: Array[String]) { ...
- 关于Natively Compiled Stored Procedures的优化
Interpreted Transact-SQL stored procedures are compiled at first execution, in contrast to natively ...