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 * ...
随机推荐
- [转]使用@Test 也可以从spring容器中获取依赖注入
转自:http://blog.csdn.net/u010987379/article/details/52091790 @RunWith(SpringJUnit4ClassRunner.class) ...
- java - 并发编程易错实例
生产者消费者问题 https://juejin.im/post/5aeec675f265da0b7c072c56 notify()发生在wait()之前会怎么样?怎么处理? wati()等待条件的变化 ...
- Python全栈学习_day003作业
day3作业及默写 1,有变量name = "aleX leNb" 完成如下操作: 1) 移除 name 变量对应的值两边的空格,并输出处理结果 print(name.strip( ...
- web print
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- CSS属性disabled和readonly的区别是什么
在博客园中看到这样一篇文章,关于disabled和readonly的区别,以前还真的没有注意它们的区别,还是有必要知道它们的区别的,所以转载了. 这两个属性有类似之处,但是区别也是巨大的,之所以说类似 ...
- SuperMap iServer 扩展/JAVA API 系列博客整理
转载:http://blog.csdn.net/supermapsupport/article/details/70158940 SuperMap iServer为广大用户提供了整套 SDK,应用开发 ...
- 安卓开发ScrollView嵌套ListView只显示一行
在用列表控件做一个“更多功能”的界面的时候 <?xml version="1.0" encoding="utf-8"?> <ScrollVie ...
- beta冲刺随笔集
团队成员 郑西坤 031602542 (队长) 陈俊杰 031602504 陈顺兴 031602505 张胜男 031602540 廖钰萍 031602323 雷光游 031602319 吴志鸿 03 ...
- SQL Server 2012 读写分离设置 - AlsoIn
原文转至:http://www.tuicool.com/articles/a6rmiam/ 引用: http://technet.microsoft.com/zh-cn/library/jj16176 ...
- 【锁】Oracle死锁(DeadLock)的分类及其模拟
[锁]Oracle死锁(DeadLock)的分类及其模拟 1 BLOG文档结构图 2 前言部分 2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不 ...