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 10Sample Output 1:
2Sample Input 2:
1 ab 1 2Sample 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。
代码
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[12],str2[12];
13 char *p1,*p2;
14 while(scanf("%s %s",str1,str2) != EOF){
15 scanf("%d%I64d",&tag,&radix);
16 if(tag == 1){
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) + 1;
26 if (radixMin < 2)
27 radixMin = 2;
28 int strN = strlen(p2);
29 if(strN == 1){
30 N2 = char2num(*p2);
31 if(N2 == N1){
32 printf("%I64d\n",N2+1);
33 }
34 else{
35 printf("Impossible\n");
36 }
37 continue;
38 }
39 long long i = radixMin;
40 N2 = calculateFunc(p2,i);
41 int flag = 0;
42 while(N2 <= N1){
43 if(N1 == N2){
44 printf("%I64d\n",i);
45 flag = 1;
46 break;
47 }
48 i *= 2;
49 N2 = calculateFunc(p2,i);
50 }
51 if(!flag){
52 long long s = i / 2,e = i;
53 long long mid;
54 while(e >= s){
55 mid = (s + e) / 2;
56 N2 = calculateFunc(p2,mid);
57 if(N1 == N2){
58 printf("%I64d\n",mid);
59 flag = 1;
60 break;
61 }
62 else if (N1 > N2){
63 s = mid + 1;
64 }
65 else{
66 e = mid - 1;
67 }
68 }
69 if(!flag){
70 printf("Impossible\n");
71 }
72 }
73 }
74 return 0;
75 }
76
77 long long calculateFunc(char *p,long long radix)
78 {
79 int n = strlen(p);
80 int i;
81 long long N = 0;
82 for(i=0;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 = -1;
93 int t;
94 for(i=0;i<n;++i){
95 t = char2num(*(p+i));
96 if (t > m)
97 m = t;
98 }
99 return m;
100 }
101
102 int char2num(char c)
103 {
104 if(c >= '0' && c <= '9')
105 return c - '0';
106 else if(c >= 'a' && c <= 'z')
107 return c - 'a' + 10;
108 else
109 return -1;
110 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合终身会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一则复杂 SQL 改写后有感
· golang中写个字符串遍历谁不会?且看我如何提升 50 倍
· C# 代码如何影响 CPU 缓存速度?
· 智能桌面机器人:使用 .NET 为树莓派开发 Wifi 配网功能
· C# 模式匹配全解:原理、用法与易错点
· 一则复杂 SQL 改写后有感
· 曾经风光无限的 Oracle DBA 已经落伍了吗?
· 接口被刷百万QPS,怎么防?
· C# 锁机制全景与高效实践:从 Monitor 到 .NET 9 全新 Lock
· 一个开源免费、功能丰富的 WPF 自定义控件资源库