1010 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

算法说明:

已知一个数和其基数,求另一个数的基数使得这两个数相等。数字表示使用[0-9a-z],很容易看出基数的范围是[2-36],如果已知数很大,基数是会超出36的,可能会很大很大,用long long 来存储这个基数,如果你用暴力遍历查找基数,时间会超时,可以用二分查找。

#include <iostream>
#include <cstring>
using namespace std;
#define Max 3
// 转成十进制
long long toDecimal(char *N,long long radix){
long long decimal=0;
for(int i=0;i<strlen(N);i++){
if(N[i]<58)
decimal=decimal*radix+(N[i]-48);
else
decimal=decimal*radix+(N[i]-87); // a-z ,10-35 呵呵
}
return decimal;
}
//找出串中最大值
int maxValueStr(char *N){
int max=0;
for(int i=0;i<strlen(N);i++)
if(N[i]<58&&N[i]-48>max)
max=N[i]-48;
else if(N[i]-87>max)
max=N[i]-87;
if(max>1)
return max;
else
return 1;
}
int compare(char *N,long long radix,long long target){
long long decimal=0;
for(int i=0;i<strlen(N);i++){
if(N[i]<58)
decimal=decimal*radix+(N[i]-48);
else
decimal=decimal*radix+(N[i]-87);
if(decimal>target||decimal<0)
return 1;
}
if(decimal>target)
return 1;
else if(decimal<target)
return -1;
else
return 0;
}
long long binarySearch(long long target,char *N,long long low,long long high){
long long mid=low; while(low<=high){
if(compare(N,mid,target)==1)
high=mid-1;
else if(compare(N,mid,target)==-1)
low=mid+1;
else
return mid;
mid=(low+high)/2;
}
return -1;
}
int main(int argc, char* argv[])
{
char *N[Max]; long long radix,target;
int tag;
for(int i=0;i<Max;i++)
N[i]=new char[10](); cin >> N[1] >> N[2] >> tag >> radix; target=toDecimal(N[tag],radix); //目标数转成十进制比较
tag=(tag==1)? 2:1;
long long max=(long long)maxValueStr(N[tag])+1; // 出现最大值f 进制为16 +1 if(target<=max){
long long result=binarySearch(target,N[tag],max,36);
if(result==-1)
cout<<"Impossible";
else
cout<<result;
}else{
long long result=binarySearch(target,N[tag],max,target+1);
if(result==-1)
cout<<"Impossible";
else
cout<<result;
}
return 0;
}

2020考研打卡第十三天,星辰之变,骄阳岂是终点。

我要争一口气,不是想证明我了不起,我是要告诉大家,我失去的东西一定要拿回来。

PAT-1010 Radix的更多相关文章

  1. PAT 1010 Radix(X)

    1010 Radix (25 分)   Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...

  2. 已经菜到不行了 PAT 1010. Radix (25)

    https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...

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

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

  4. PAT 1010 Radix (二分)

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

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

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

  6. PAT甲级1010. Radix

    PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...

  7. PAT 解题报告 1010. Radix (25)

    1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...

  8. PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  9. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  10. PAT甲组 1010 Radix (二分)

    1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...

随机推荐

  1. 记录一次mysql使用load into命令导入csv格式数据的过程

    今天从qwiklab实验获取一组数据,大概有5万条,在qwiklab实验室使用的是pgsql数据库,但是今天想把他插入本地的mysql数据库中. 1.首先是查看一下数据内容: 数据中有的是空值,有的是 ...

  2. October 27th, 2017 Week 43rd Friday

    The only thing predictable about life is its unpredictability. 人生唯一可以预知的,就是它的变化莫测. Is it really unpr ...

  3. October 14th 2017 Week 41st Saturday

    I was well beaten myself, and I am beffer for it. 我自己也被打败过,但我因此变得更好. For most of us, the life road c ...

  4. 联想笔记本BIOS设置中文详解

    对于很多新装系统的小伙伴们 可能很多都不是太懂BIOS中都是干什么用的,小编这里给大家详细介绍一下 联想笔记本的主板BIOS设置跟别的笔记本或许有些不同但大体相差不多,和大家分享一下. BIOS介绍 ...

  5. 027.1 反射技术 Class

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反 ...

  6. 数据结构习题Pop Sequence的理解----小白笔记^_^

    Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, ...

  7. C语言:值传递,地址传递和引用传递(example:值交换)

    于C语言中值传递.地址传递和引用传递的我个人理解. 通过一个例子:swap(交换两个整型变量的值)来表现! #include <stdio.h> void swap1(int* a,int ...

  8. oracle 查询SQL 的执行速度

    SELECT SE.SID,       OPNAME,       TRUNC(SOFAR / TOTALWORK * 100, 2) || '%' AS PCT_WORK,       ELAPS ...

  9. HDU1875+Prim模板

    https://cn.vjudge.net/problem/HDU-1875 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府 ...

  10. 浅谈 MVC 和 MTV

    浅谈 MVC 和 MTV 一.MVC M:model,模型,就是数据模型,负责数据的存取: V:view,视图,负责页面的展示逻辑: C:controller,控制器,负责业务逻辑的处理: 二.MTV ...