您的位置:首页 > 其它

1010. Radix (25)

2015-11-22 00:47 309 查看
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最大是36,结果好几个点死活通不过,后来发现Radix有可能很大,所以就加大了范围。

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>

#define Maxn 11

char input[2][Maxn];
int lengthofinput[2];
long long int minradix[2];

int MinPossibleRadix(char input[]);
long long int ConvertToDecimal(char input[],int length,long long int radix);

int main(void)
{
int Tag;
long long int Radix;
scanf("%s %s %d %lld",input[0],input[1],&Tag,&Radix);

lengthofinput[0] = strlen(input[0]);
lengthofinput[1] = strlen(input[1]);
minradix[0] = MinPossibleRadix(input[0]);
minradix[1] = MinPossibleRadix(input[1]);

long long int DefiniteSum = ConvertToDecimal(input[Tag-1],lengthofinput[Tag-1],Radix);

long long int leftradix = minradix[2-Tag];
long long int rightradix = DefiniteSum + 1;
long long int midradix = 0;
long long int  thissum = 0;
bool equal = false;
while(leftradix <= rightradix)
{
midradix = (leftradix + rightradix) / 2;
thissum = ConvertToDecimal(input[2-Tag],lengthofinput[2-Tag],midradix);
if(thissum > DefiniteSum || thissum == -1)
{
rightradix = midradix - 1;
}
else if(thissum < DefiniteSum)
{
leftradix = midradix + 1;
}
else
{
equal = true;
break;
}
}

if(equal)
printf("%lld",midradix);
else
printf("Impossible");
system("pause");
return 0;
}

int MinPossibleRadix(char input[])
{
int length = strlen(input);
int minradix = 0;
for(int i=0;i<length;++i)
{
if(isdigit(input[i]))
{
input[i] = input[i] - '0';
}
if(islower(input[i]))
{
input[i] = input[i] - 'a' +10;
}
if(input[i] > minradix)
minradix = input[i];
}

return minradix+1;
}

long long int ConvertToDecimal(char input[],int length,long long int radix)
{
long long int sum = 0;
for(int i=0;i<length;++i)
{
sum = sum*radix + input[i];
if(sum < 0)
return -1;
}
return sum;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: