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 ...
随机推荐
- C/C++程序计时函数gettimeofday的使用
linux 环境下 用 clock_t发现不准. 换用 //头文件 #include <sys/time.h> //使用timeval start, end; gettimeofday ...
- [转]MVC 检测用户是否已经登录
本文转自:http://blog.csdn.net/jayzai/article/details/41252137 当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null) ...
- html/css实现聊天布局
效果图 项目结构 html代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" / ...
- ES-自然语言处理之中文分词器
前言 中文分词是中文文本处理的一个基础步骤,也是中文人机自然语言交互的基础模块.不同于英文的是,中文句子中没有词的界限,因此在进行中文自然语言处理时,通常需要先进行分词,分词效果将直接影响词性.句法树 ...
- mongodb的安装及配置安装服务
1. 安装mongodb数据库 mongodb官方网址:https://www.mongodb.org 安装好之后的步奏: 第一步:规划你的安装目录和数据库文件的存储路径,我打算将Mongo的程序文件 ...
- hdu 3232 Crossing Rivers 过河(数学期望)
题意:你在点A,目的地是点B,A和B的距离为D.中间隔了好多条河(所有河不会重叠),每条河有3个参数(P,L,V),其中P表示距离A点的长度,L表示河的长度,V表示河里的船的速度.假设每条河中仅有1条 ...
- docker 容器的网络
容器的网络模式 bridge -net=bridge 默认网络.docker启动后创建一个docker0网桥,默认创建的容器也添加到这个网桥 [root@localhost ~]# ip a 1: l ...
- SQL Server数据库的除法默认向下取整,要返回小数的解决方法
num1; / 1000.0 num2; * 1.0 num3; num4; 结果:
- springmvc请求方法那些事
@RequestMapping 用法详解之地址映射 (2013-08-11 16:06:58) 转载▼ 标签: it 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式 ...
- 如何用纯 CSS 创作一个冒着热气的咖啡杯
效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/xjXxoz 可交互视频教程 此视 ...