PTA (Advanced Level) 1010 Radix
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,给出其中一个数字tag的进制radix,要求判断是否存在某一进制可以使另一个数字与给定数字相等。
为了方便运算,用字符串tn1、tn2记录输入的两个数字,字符串n1记录给定进制的数字,字符串n2记录未确定数字。用map<char, int > mp,记录每个字符所对应的数值,之后,可以先将给定进制的数字n1转化为10进制,n2的进制最小为其包含的最大数字+1记为leftn,且由于n2是整数,所以其进制最大不会超过n1的十进制与leftn中较大的一个+1,记为rightn。以leftn和rightn分别为左右边界二分所有进制,记mid为中点进制,将n2按mid进制转化为10进制与n1的十进制进行比较,如果n2较大证明mid取值过大,将rightn记为mid - 1;若小了,证明mid取值过小,leftn记为mid+1,若正好相等则找到答案。若无法找到某进制使得n1与n2相等,返回-1,如果返回的答案不为-1,输出答案,否则输出Impossible。
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
map<char, LL> mp;
void init(){
for(char i = ''; i <= ''; i++){
mp[i] = i - ''; //初始化0 - 9
}
for(char i = 'a'; i <= 'z'; i++){
mp[i] = i - 'a' + ; //初始化a - z
}
}
LL toDecimal(string a, LL radix, LL maxn){ //转化为10进制的函数,所转化后的数不会超过给出的maxn
int len = a.size();
LL ans = ;
for(int i = ; i < len; i++){
ans = ans * radix + mp[a[i]];
if(ans < || ans > maxn){ //如果数据溢出了或超过上限
return -;//返回-1
}
}
return ans;//返回转化后的值
}
int cmp(string a, LL radix, LL n1){ //比较函数,用于比较n2的radix进制转化为10进制后与n1的十进制的大小
LL n2_10 = toDecimal(a, radix, n1);
//获得n2转化为10进制的值
if(n2_10 == n1) //如果n2的10进制与n1的十进制相同证明该进制是我们要获得的进制,返回0
return ;
else if(n2_10 < ) //如果toDecimal函数返回的n2小于0,证明n2在该进制下转化为十进制后大于n1的十进制
return ; //进制过大返回1
else if(n1 > n2_10) //如果n2在当前进制下转化为10进制小于n1的十进制
return -; //进制过小返回-1
else //否则返回1
return ;
}
LL getRadix(string a, LL leftn, LL rightn, LL n1){
//二分函数传入n2字符串,最小进制,最大进制,n1的十进制值
while(leftn <= rightn){
LL mid = (leftn + rightn) / ;
//获得中点
LL flag = cmp(a, mid, n1);
//判断终点进制n1与n2状态
if(flag == ) //若比较函数返回了0,证明在mid进制下n1与n2相等
return mid; //返回mid
else if(flag == -){ //进制过小
leftn = mid + ;
}else if(flag == ){ //进制过大
rightn = mid - ;
}
}
return -;
}
int getMaxNum(string a){ //获得n2中最大的数字
LL ans = -;
for(string::iterator it = a.begin(); it != a.end(); it++){
ans = max(ans, mp[*it]);
}
return ans;
}
int main(){
init(); //初始化mp
string tn1, tn2, n1, n2;
int tag, radix;
cin >> tn1 >> tn2 >> tag >> radix;
//输入 tn1 tn2 tag radix;
if(tag == ){
n1 = tn1;
n2 = tn2;
}else{
n1 = tn2;
n2 = tn1;
}
//n1记录已经确定进制的数字,n2记录未确定的数字
LL n1_10 = toDecimal(n1, radix, INT_MAX);
//将n1转化为10进制其上限为无穷大
LL leftn = getMaxNum(n2) + ;
//获得n2的最小进制
LL rightn = max(leftn, n1_10) + ;
//获得n2的最大进制
LL ans = getRadix(n2, leftn, rightn, n1_10);
//二分所有进制
if(tn1 == tn2)
printf("%d\n", radix);
else if(ans == -){
printf("Impossible\n");
}else{
printf("%lld\n", ans);
}
return ;
}
PTA (Advanced Level) 1010 Radix的更多相关文章
- PAT (Advanced Level) 1010. Radix (25)
撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...
- PTA(Advanced Level)1036.Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA (Advanced Level) 1027 Colors in Mars
Colors in Mars People in Mars represent the colors in their computers in a similar way as the Earth ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- PTA (Advanced Level) 1019 General Palindromic Number
General Palindromic Number A number that will be the same when it is written forwards or backwards i ...
- PTA (Advanced Level) 1015 Reversible Primes
Reversible Primes A reversible prime in any number system is a prime whose "reverse" in th ...
- PTA(Advanced Level)1025.PAT Ranking
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- PTA (Advanced Level)1035.Password
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...
随机推荐
- Javascript设计模式理论与实战:工厂方法模式
本文从简单工厂模式的缺点说起,引入工厂方法模式,介绍的工厂方法模式的基本知识,实现要点和应用场景,最后举例进行说明工厂方法模式的应用.在之前的<Javascript设计模式理论与实战:简单工厂模 ...
- XEvent--Demo--使用XEvent来捕获在数据库DB1上发生的锁请求和锁释放
--==============================================================--使用XEvent来捕获在数据库DB1上发生的锁请求和锁释放--=== ...
- Tempdb--Snapshot
The insert operation does not cause a row version to be generated because there is really no prvious ...
- centos7怎能开机设置文本界面
rm -f /etc/systemd/system/default.target 设置命令行级别方法:ln -sf /lib/systemd/system/runlevel3.target /etc/ ...
- Sql语法高级应用之六:如何在Sql语句中如何使用TRY...CATCH
TRY...CATCH使用范例 BEGIN TRY //逻辑语句块 END TRYBEGIN CATCH //Catch异常处理块 SET @msg = ERROR_MESSAGE(); PRINT ...
- LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS M ...
- 浅谈由管理者角色引出的B端产品设计思考点
本文来自网易云社区 作者:任琼瑶 最近一直都在持续跟进云课堂B端的交互设计,在此期间,多多少少遇到了一些不曾遇到过的问题,虽然同是做产品设计,但B端和C端产品的确存在很多不同之处. 首先,当我们在做C ...
- leetcode 84. 柱状图中最大的矩形 JAVA
题目: 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高 ...
- [Objective-C语言教程]复合对象(33)
在Objective-C中,可以在类集群中创建子类,该类集合定义了一个嵌入在其中的类. 这些类对象是复合对象.你可能想知道什么是类集群,下面首先了解什么是类集群. 1. 类集群 类集群是基础框架广泛使 ...
- Linux动态库的导出控制
在实际工作中,许多软件模块是以动态库的方式提供的.做为模块开发人员,我们不仅要掌握如何编写和构建动态库,还要了解如何控制动态库的导出接口,这样,我们可以向模块的用户仅导出必要的接口,而另一些内部接口, ...