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 ...
随机推荐
- Oracle_spatial的空间操作符介绍
空间操作符 一.主要空间操作符 1.SDO_FILTER SDO_FILTER(geometry1, geometry2, param); 判断两个几何体是否有相交 SELECT c.mkt_id, ...
- Spring-IOC RootBeanDefinition源码分析
GenericBeanDefinition是一站式的标准bean definition,除了具有指定类.可选的构造参数值和属性参数这些其它bean definition一样的特性外,它还具有通过par ...
- cmd那个命令是查看端口情况的?
netstat -a查看开启哪些端口netstat -n查看端口的网络连接情况netstat -v查看正在进行的工作netstat -p tcp/ip查看某协议使用情况netstat -s 查看正在使 ...
- 静态性能测试-hc课堂笔记
UI自动化,需要掌握html相关知识 w3c网站. 会了性能测试就会了接口自动化. 静态扫描:降低40-50% findbugs,隐含的bug checkstyle,风格规范 域名解析: 输入网址-D ...
- 如何将tensor的内容输出到文本文件
local part2 = self.convModel:forward({linputs, rinputs}) ) local file = io.open('/home/xbwang/Deskto ...
- APR Recipe
1, pt fix 后生成的 icctcl,回到 icc source 后,如果需要eco_route,建议先关掉 timing_driven,因为 icc 与 pt 看到的 timing 情况一般 ...
- 八,ESP8266 文件保存数据(基于Lua脚本语言)
https://www.cnblogs.com/yangfengwu/p/7533845.html 应该是LUA介绍8266的最后一篇,,,,,,下回是直接用SDK,,然后再列个12345...... ...
- setInterval() 方法应用
setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭.由 s ...
- 1.5《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——清屏
使用命令行时,使用clear命令清除屏幕非常方便: $ clear 键盘简写为^L.(Ctrl + L) 同样地,使用完终端当前窗口或标签页,使用exit命令退出进程: $ exit 键盘简写为^D ...
- Spark笔记(一):错误总结
1.转义字符: 常见的replaceAll,split,mkstring中涉及到特殊字符的都要加上转义字符,比如str.split("\\|"),str.replaceAll(&q ...