PAT-1010 Radix
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的更多相关文章
- PAT 1010 Radix(X)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...
- 已经菜到不行了 PAT 1010. Radix (25)
https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...
- PAT 1010 Radix 进制转换+二分法
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT 1010 Radix (二分)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 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甲级1010. Radix
PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...
- PAT 解题报告 1010. Radix (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- 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 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- PAT甲组 1010 Radix (二分)
1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...
随机推荐
- [BZOJ 1568][JSOI2008]Blue Mary开公司
[BZOJ 1568][JSOI2008]Blue Mary开公司 题意 \(n\) 次操作, 维护一个一次函数集合 \(S\). 有两种操作: 给定 \(b\) 和 \(k\), 向 \(S\) 中 ...
- 2018.08.31 19:41 自学go语言
有的人是从最基础的开始学,而我却是从最简单开始学,学着调试,学着编程,其实我也是编程小白,好多的不懂,我不明白很多都可以用云完成了,为什么还要继续自己编程,不明白,但是有需求吧,有需求是件好事情,说明 ...
- [ML学习笔记] 决策树与随机森林(Decision Tree&Random Forest)
[ML学习笔记] 决策树与随机森林(Decision Tree&Random Forest) 决策树 决策树算法以树状结构表示数据分类的结果.每个决策点实现一个具有离散输出的测试函数,记为分支 ...
- BZOJ3251:树上三角形(乱搞)
Description 给定一大小为n的有点权树,每次询问一对点(u,v),问是否能在u到v的简单路径上取三个点权,以这三个权值为边长构成一个三角形.同时还支持单点修改. Input 第一行两个整数n ...
- 动态显示checkbox选中条数
<script> $('input[type=checkbox]').click( function () { $('span#cheak_len').empty(); var len = ...
- [CTSC2018]混合果汁
题目连接:https://www.luogu.org/problemnew/show/P4602 因为题中说是让最小值最大,所以自然想到二分答案.对于每一个二分的值,判断是否合法,若合法,在右区间二分 ...
- 静态性能测试-hc课堂笔记
UI自动化,需要掌握html相关知识 w3c网站. 会了性能测试就会了接口自动化. 静态扫描:降低40-50% findbugs,隐含的bug checkstyle,风格规范 域名解析: 输入网址-D ...
- shiro实战系列(一)之入门实战
一.什么是shiro? Apache Shiro 是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权,企业会话管理和加密. Apache Shiro 的首要目标是易于使用和理解.安全有 ...
- JVM内存管理及GC机制
一.概述 Java GC(Garbage Collection,垃圾收集,垃圾回收)机制,是Java与C++/C的主要区别之一,作为Java开发者,一般不需要专门编写内存回收和垃圾清理代码,对内存泄露 ...
- PAT B1035 插入与归并 (25 分)
根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭 ...