PAT 1010
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
刚开始以为此题的范围会超过long long int,试过后发现并没有。另外题目说字符串里面的数字只有{0-9,a-z},让我以为radix的范围不会超过36,后来发现错了。更改后提交,发现有一个case超时了,估计这个case里的radix非常大,于是radix也采用long long int,并且采用二分搜索,这才解决超时问题。注意这VS下long long int应该定义成__int64,输入输出格式为%I64d。
代码
1 #define __int64 long long
2 #include <stdio.h>
3 #include <string.h>
4
5 long long calculateFunc(char *,long long);
6 int maxDigit(char *);
7 int char2num(char);
8 int main()
9 {
10 long long N1,N2,radix;
11 int tag;
12 char str1[],str2[];
13 char *p1,*p2;
14 while(scanf("%s %s",str1,str2) != EOF){
15 scanf("%d%I64d",&tag,&radix);
16 if(tag == ){
17 p1 = str1;
18 p2 = str2;
19 }
20 else{
21 p1 = str2;
22 p2 = str1;
23 }
24 N1 = calculateFunc(p1,radix);
25 int radixMin = maxDigit(p2) + ;
26 if (radixMin < )
27 radixMin = ;
28 int strN = strlen(p2);
29 if(strN == ){
30 N2 = char2num(*p2);
31 if(N2 == N1){
32 printf("%I64d\n",N2+);
33 }
34 else{
35 printf("Impossible\n");
36 }
37 continue;
38 }
39 long long i = radixMin;
40 N2 = calculateFunc(p2,i);
41 int flag = ;
42 while(N2 <= N1){
43 if(N1 == N2){
44 printf("%I64d\n",i);
45 flag = ;
46 break;
47 }
48 i *= ;
49 N2 = calculateFunc(p2,i);
50 }
51 if(!flag){
52 long long s = i / ,e = i;
53 long long mid;
54 while(e >= s){
55 mid = (s + e) / ;
56 N2 = calculateFunc(p2,mid);
57 if(N1 == N2){
58 printf("%I64d\n",mid);
59 flag = ;
60 break;
61 }
62 else if (N1 > N2){
63 s = mid + ;
64 }
65 else{
66 e = mid - ;
67 }
68 }
69 if(!flag){
70 printf("Impossible\n");
71 }
72 }
73 }
74 return ;
75 }
76
77 long long calculateFunc(char *p,long long radix)
78 {
79 int n = strlen(p);
80 int i;
81 long long N = ;
82 for(i=;i<n;++i){
83 N = N * radix + char2num(*(p+i));
84 }
85 return N;
86 }
87
88 int maxDigit(char *p)
89 {
90 int n = strlen(p);
91 int i;
92 int m = -;
93 int t;
94 for(i=;i<n;++i){
95 t = char2num(*(p+i));
96 if (t > m)
97 m = t;
98 }
99 return m;
}
int char2num(char c)
{
if(c >= '' && c <= '')
return c - '';
else if(c >= 'a' && c <= 'z')
return c - 'a' + ;
else
return -;
}
PAT 1010的更多相关文章
- PAT 1010 一元多项式求导 (25)(STL-map+思路)
1010 一元多项式求导 (25)(25 分)提问 设计函数求一元多项式的导数.(注:x^n^(n为整数)的一阶导数为n*x^n-1^.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均 ...
- 已经菜到不行了 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. 一元多项式求导
1010. 一元多项式求导 (25) 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数 ...
- PAT 1010 Radix(X)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...
- PAT 1010. 一元多项式求导 (25)
设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式:以与 ...
- PAT——1010. 一元多项式求导
设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式:以与 ...
- 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 ...
随机推荐
- 理解$watch ,$apply 和 $digest --- 理解数据绑定过程
原文地址:http://angular-tips.com/blog/2013/08/watch-how-the-apply-runs-a-digest/ 注 这篇博文主要是写给新手的,是给那些刚刚开始 ...
- 也用 Log4Net 之走进Log4Net (四)
转载地址:http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html 我是转的别人的内容,我觉得他写的非常好,所以我把其中三分之二转了过来 ...
- Project Euler 26 Reciprocal cycles
题意:求1到n中所有数的倒数中循环小数循环体最长的数 解法:如果一个数的质因子只有2和5,那么这个数的倒数一定不是一个循环小数.如果一个数含有质因子2或5,那么它的循环体和去掉质因子2和5之后的数的循 ...
- 【原创】Linux下使用SecureCRT的方法:破解&编码
1.下载SecureCRT软件 ubuntu64位:http://download.csdn.net/detail/cobps/7941145 ubuntu32位:http://download.cs ...
- HDU 4081 Qin Shi Huang's National Road System 最小生成树
分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...
- Selenium2Library中的Get Alert Message
今天在处理页面的弹出框(alert)时,发现Get Alert Message 并不如字面意思这么简单 函数说明如下: 很明了:(1)返回alert 的text (2)如果没有alert,则该keyw ...
- 【转】C数据存储(包括const存储在哪,C++不同部分我在文中用红字已指出)
非原创(文中红字为自己见解,如有不对,请大神指点) 程序由指令和数据组成,C语言程序亦是如此.开发者在编写程序的时候往往需要根据不同数据的特点以及程序需求来选择不同的数据存储方式,那么在C语言中数据的 ...
- 集群搭建必备:虚拟机之一实现Host-only方式上网
Host-only模式实现联网得考虑如下配置过程: 1. 安装VMware-Workstation,安装虚拟机Linux(centos.ubuntu等)完毕: 2.设置虚拟机上网方式是Host-onl ...
- 关于main()和_tmain()
1.两者的共同点 int _tmain(int argc, _TCHAR* argv[]) 和 int main(int argc, char* argv[]) ,两者都是程序的主函数,两者 ...
- 荔枝FM架构师刘耀华:异地多活IDC机房架构 - 极客头条 - CSDN.NET
荔枝FM架构师刘耀华:异地多活IDC机房架构 - 极客头条 - CSDN.NET 荔枝FM架构师刘耀华:异地多活IDC机房架构 - 极客头条 - CSDN.NET 途牛谭俊青:多数据中心状态同步&am ...