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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
long long binarySearch();
int cmp( long long k);
char a[],b[],c[];
long long ans[];
long long low,high,len,valuea;
int main()
{
int tag;
long long radixa,temp,ret;
int i;
scanf("%s %s %d %lld",a,b,&tag,&radixa);
if( tag==)
{
strcpy(c,a);
strcpy(a,b);
strcpy(b,a);
}
for( i=; a[i]!='\0'; i++)
{
if( a[i]>='' && a[i]<='')
temp = a[i]-'';
else if( a[i]>'a' && a[i]<'z')
temp = a[i]-'a'+;
valuea = valuea*radixa + temp;
} for( i=; b[i]!='\0'; i++)
{
if( b[i]>='' && b[i]<='')
temp = b[i]-'';
else if( b[i]>'a' && b[i]<'z')
temp = b[i]-'a'+;
ans[i]=temp;
if( low<temp)
low = temp;
}
low++;
len = strlen(b);
if( low>valuea)
high = low+;
else high=valuea+;
ret = binarySearch();
if( ret==-)
printf("Impossible\n");
else printf("%lld\n",ret);
return ;
}
long long binarySearch()
{
long long l=low,h=high,mid;
while( l<=h )
{
mid = (l+h)/;
if(cmp(mid)==)
return mid;
else if(cmp(mid)<)
l= mid+;
else h=mid-;
}
return -;
} int cmp( long long k)
{
long long valueb=;
int i;
for( i=; i<len; i++)
valueb = k*valueb+ans[i];
if( valueb< || valueb>valuea)
return ;
else if( valueb<valuea)
return -;
else if ( valuea==valueb)
return ;
}
1010. Radix (25)(出错较多待改进)的更多相关文章
- 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 (25)
https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...
- 1010. Radix (25)(未完成)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 1010. Radix (25)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT (Advanced Level) 1010. Radix (25)
撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...
- 1010. Radix (25) pat
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 1010 Radix (25)(25 point(s))
problem Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true ...
随机推荐
- SpringCloud开发学习总结(八)—— API网关服务Zuul(一)
大多数情况下,为了保证对外服务的安全性,我们在服务端实现的为服务接口时往往都会有一定的权限校验机制,比如对用户登录状态的校验等:同时为了防止客户端在发起请求时被篡改等安全方面的考虑,还会有一些签名校验 ...
- log4go折腾
导包 go get -u github.com/alecthomas/log4go log4go.xml配置 <logging> <filter enabled="true ...
- maven编译报错 -source 1.5 中不支持 lambda(或diamond) 表达式,编码 UTF-8 的不可映射字符
在用maven编译项目是由于项目中用了jdk 1.8, 编译是报错 -source 1.5 中不支持 lambda 表达式. 错误原因: Maven Compiler 插件默认会加 -source ...
- MySQL 当记录不存在时insert,当记录存在时更新
网上基本有三种解决方法. 第一种: 示例一:insert多条记录 假设有一个主键为 client_id 的 clients 表,可以使用下面的语句: INSERT INTO clients (clie ...
- The lion king 经典句型摘录
What am I going to do with him? Everything the light touches is our kingdom. But I thought a king ca ...
- 安装scount的es驱动,composer require tamayo/laravel-scout-elastic报错解决
执行 composer require tamayo/laravel-scout-elastic 报错信息如下: Problem 1 - Installation request for tamayo ...
- 如何处理Docker的错误消息request canceled:Docker代理问题
在本地安装Kubernetes时,遇到错误消息: request canceled while waiting for connection(Client.Timeout exceeded while ...
- varchar2(100 char)是什么意思
最佳答案 varchar2(100 char)最长可以插入100个任意字符而varchar2(100)最长可以插入100个英文字符
- centos6上安装mysql8.0版本
本博客是采用yum源的方式安装,非常的方便和快捷.(redhat 与centos7 等操作系统都可以采用此方法,步骤大体一致) mysql官网地址: https://dev.mysql.com 开 ...
- JQQ文字素材
1.十二生肖:子鼠.丑牛.寅虎.卯兔.辰龙.巳舍.午马.未羊.申猴.酉鸡.戌狗.亥猪.丙申年(2016)乙未年(2015)甲午年(2014)癸巳年(2013)壬辰年(2012)辛卯年(2011)庚寅年 ...