PAT (Advanced Level) Practise 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
注意
- 题目中的radix范围可能很大,不一定仅在1~36之间
- 需要确定未指明radix的数的radix可能范围.下限可以通过遍历该数,得到其中最大的字符max得到;上限可以通过比较max和另外一个数中的最大者求得.
- 实际编程发现,按radix顺序比较两个数会超时,并且考虑到radix单调递增,故可以使用二分法求解.
- 在比较两个数时,如果将radix对应的数算出后再比较,则会发生溢出,故需要在将radix对应的数从字符串转换成长整型过程中比较.
- 变量类型: 根据题目易知number1 number radix均应该使用
long long int
型
编程实现
#include <stdio.h>
#include <string.h>
#define NotFound -1
typedef long long int LL;
LL cal2radix10(const char *num, LL radix);
int cmp(LL number1, const char *str_num2, LL radix);
LL IsEqual(const char *num1, LL radix, const char *num2);
int max_digit(const char *s);
LL max(LL number1, LL number2);
int main(void)
{
char N1[11], N2[11];
int tag;
LL radix;
LL equal;
scanf("%s %s %d %lld", N1, N2, &tag, &radix);
if ( strcmp(N1,N2) == 0 ) {
if ( strcmp(N1,"1") == 0 )
printf("2\n");
else
printf("%lld\n", radix);
return 0;
}
if ( tag == 1 )
equal = IsEqual(N1, radix, N2);
else if ( tag == 2 )
equal = IsEqual(N2, radix, N1);
if ( equal > 0 )
printf("%lld\n", equal);
else
printf("Impossible\n");
return 0;
}
LL max(LL number1, LL number2)
{
return (number1 > number2)?number1:number2;
}
int max_digit(const char *s)
{
int max = -1;
int tmp;
while ( *s != '\0' ) {
if ( '0' <= *s && *s <= '9' )
tmp = *s - '0';
else if ( 'a' <= *s && *s <= 'z' )
tmp = *s - 'a' + 10;
if ( max < tmp ) max = tmp;
s++;
}
return max;
}
LL IsEqual(const char *num1, LL radix, const char *num2)
{
int res;
LL Low, High, Mid;
LL number1;
number1 = cal2radix10(num1,radix);
Low = max_digit(num2)+1;
High = max(Low, number1);
while ( Low <= High )
{
Mid = Low + ((High-Low)>>1);
res = cmp(number1, num2, Mid);
if ( res < 0 )
High = Mid - 1;
else if ( res > 0 )
Low = Mid + 1;
else
return Mid;
}
return NotFound;
}
int cmp(LL number1, const char *str_num2, LL radix)
{
int i, bit, n;
LL digit = 1, number2 = 0;
n = strlen(str_num2);
for ( i = n-1; i >= 0; i-- ) {
if ( '0' <= str_num2[i] && str_num2[i] <= '9' )
bit = str_num2[i] - '0';
else if ( 'a' <= str_num2[i] && str_num2[i] <= 'z' )
bit = str_num2[i] -'a' + 10;
number2 += bit*digit;
if ( number1 < number2 ) return -1;
digit *= radix;
}
if ( number1 > number2 )
return 1;
else
return 0;
}
LL cal2radix10(const char *num, LL radix)
{
int i, n;
LL s = 0;
LL bit;
LL digit = 1;
n = strlen(num);
for ( i = n-1; i >= 0; i-- ) {
if ( '0' <= num[i] && num[i] <= '9' )
bit = num[i] - '0';
else if ( 'a' <= num[i] && num[i] <= 'z' )
bit = num[i] - 'a' + 10;
s += bit * digit;
digit *= radix;
}
return s;
}