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. Spark 集群搭建

    0. 说明 Spark 集群搭建 [集群规划] 服务器主机名 ip 节点配置 s101 192.168.23.101 Master s102 192.168.23.102 Worker s103 19 ...

  2. Linux命令网络命令之netstat

    Linux命令网络命令之netstat 这一年感觉到技术上成长到了一个瓶颈.可能是感觉自己学的东西足够应付目前的工作了,因此精神上就产生了懈怠,不思进取.到了一个技术氛围不错的公司,有许多专业能力很不 ...

  3. centos中,tomcat项目创建文件的权限

    参考文章:https://cloud.tencent.com/info/5f02caa932fd6dbfc46a3bb01af135e0.html 我们在centos中输入umask,会看到输出002 ...

  4. Alpha课堂展示(麻瓜制造者)

    目录 成员简介 演示动态图 预期用户量 演示动态图 目标用户视频 分工协作 项目管理 质量控制 团队角色与具体贡献 用户反馈 成员简介 刘双玉 http://www.cnblogs.com/liu42 ...

  5. 自己写的开源MVC-easyMVC分享

    简介 基本风格是按照spring mvc做的,在后期会加入一些新的特性,封装成易于自己项目使用的mvc框架. github地址: https://github.com/tangyanbo/easymv ...

  6. [python] 列表解析式的高效与简洁

    方法一(列表解析式): list1 = ["abc","efg","hij"] list2 = [i[0] for i in list1] ...

  7. android studio 导入module作为lib使用

    1.将 android module导入 android project 中  2.在要作为lib导入的module 的build.gradle文件中添加一行 “apply plugin: ‘andr ...

  8. 拯救U盘之——轻松修复U盘“无法访问”的故障

    在使用U盘或者移动硬盘的过程中,大家是否和我一样,有个不好的操作习惯,明知不好但是在每次使用时都很少记得“安全删除硬件”,随手一把走人.终于出问题了,那天给mm复制完资料,拔了再插到自己的电脑上,打开 ...

  9. HackerRake平台说明和介绍

    这是之前调研的时候稍微做了一个大致的总结,现在将其分享出去,感觉放在自己的文档管理库中,用处有限.分享出去,说不定能给一些朋友带来有益的启发. 另外我们团队最近也在考虑开发OJ平台.HackerRak ...

  10. 【转】SQL 常用关键字释义和用法

    转自: http://blog.csdn.net/iamwangch/article/details/8093933     下面 是 从网络上整理 出来的 SQL 关键字和 常用函数的 释义和简单用 ...