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 * ...
随机推荐
- hadoop之 hadoop能为企业做什么?
hadoop是什么? Hadoop是一个开源的框架,可编写和运行分不是应用处理大规模数据,是专为离线和大规模数据分析而设计的,并不适合那种对几个记录随机读写的在线事务处理模式.Hadoop=HDFS( ...
- springMVC 拦截器源码解析
前言:这两天学习了代理模式,自然想到了 springmvc 的 aop 使用的就是动态代理,拦截器使用的就是 jdk 的动态代理.今天看了看源码,记录一下.转载请注明出处:https://www.cn ...
- 关于git的一个错误提示
一般clone仓库后如果,remote -v或者add出现如下的错误的话: Not a git repository (or any of the parent directories): .git需 ...
- Spring - constructor-arg和property的使用示例
一.说明 constructor-arg:通过构造函数注入. property:通过setter对应的方法注入. 二.property使用实例 1.Model代码: public cla ...
- CSS-高度塌陷问题
目录 CSS-高度塌陷问题 表现 产生的原因 高度塌陷的解决办法: BFC相关 CSS-高度塌陷问题 表现 例如: HTML: <div class="first"> ...
- Archlinux/Manjaro使用笔记-报错:一个或多个 PGP 签名无法校验!的解决方法
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 解决办法:添加无法校验的pgp签名为信任 gpg --recv-keys xxxxxx xxxxxx为无法校验的gpg值
- 浅析requests库响应对象的text和content属性
在做爬虫时请求网页的requests库是必不可少的,我们常常会用到 res = resquests.get(url) 方法,在获取网页的html代码时常常使用res的text属性: html = re ...
- 【代码笔记】Web-ionic-index创建侧边栏
一,创建侧边栏. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- JMeter 利用Jmeter批量数据库插入数据
利用Jmeter批量数据库插入数据 by:授客 QQ:1033553122 1. 启动Jmeter 2. 添加 DBC Connection Configuration 右键线程组-> ...
- matlab练习程序(高斯牛顿法最优化)
计算步骤如下: 图片来自<视觉slam十四讲>6.2.2节. 下面使用书中的练习y=exp(a*x^2+b*x+c)+w这个模型验证一下,其中w为噪声,a.b.c为待解算系数. 代码如下: ...