您的位置:首页 > 其它

1010. Radix (25)

2018-02-07 20:57 337 查看


1010. Radix (25)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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型,然后需要二分查找提高效率,不然有一个测试点过不了。

还有几种特殊情况要考虑,最小进制为2.后面顺序查找有一个测试点过不了。#include <stdio.h>
#include <string.h>

long long int Cal(char c)
{
if (c >= '0' && c <= '9')
{
return (c - '0');
}
else
return (c - 'a' + 10);
}

long long int CalValueBaseRadix(char t[], long long int radix)
{
int i;
long long int result = 0;
for (i = 0; i<strlen(t); i++)
{
result *= radix;
result += Cal(t[i]);
if (result < 0)
{
return -1;
}
}
return result;
}

int main(void)
{

char a[15], b[15];
long long tag;
long long radixA, radixB;

scanf("%s %s %lld %lld", a, b, &tag, &radixA);

if (tag == 2)
{
//swap a and b
char tmp[15];
strcpy(tmp, a);
strcpy(a, b);
strcpy(b, tmp);
}

long long minRadix, maxRadix;

//value
long long i;
long long int valueA = CalValueBaseRadix(a, radixA);

minRadix = 0;
for (i = 0; i<strlen(b); i++)
{
if (Cal(b[i]) > minRadix)
minRadix = Cal(b[i]) + 1;
}

maxRadix = valueA + 1;
while (minRadix <= maxRadix)
{
//cal
radixB = (minRadix + maxRadix) / 2;
long long int valueB = CalValueBaseRadix(b, radixB);

if (valueB == -1 || valueB > valueA)
{
maxRadix = radixB - 1;
}
else if (valueB < valueA)
{
minRadix = radixB + 1;
}
else if (valueA == valueB)
{
printf("%lld",radixB);
return 0;
}

}

printf( "Impossible");

return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char basic[36] = { '0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z'};

long long int Todec(char* num, int radix)
{
int bas,temp;
int i=0;
long long sum=0;

for (i = 0; i < strlen(num)-1; i++)
{  if (num[i] >= '0'&&num[i] <= '9')temp = num[i] - '0';
else temp = num[i] - 'a' + 10;
sum = (sum + temp)*radix;
}
if (num[i] >= '0'&& num[i] <= '9')temp = num[i] - '0';
else temp = num[i] - 'a' + 10;
sum += temp;
return sum;
}
int Findmax(char* num)
{
int i,temp;
char max = '0';
for (i = 0; i < strlen(num); i++)
{
if (num[i] > max)
max = num[i];
}
if (max >= '0'&&max <= '9')temp = max - '0';
else temp = max - 'a' + 10;
return temp;
}
long long int Findradix(char* num, long long int sum)
{
int max = Findmax(num);
int temp,i;
long long index=-1;
for (i = max+1; i < sum+2; i++)
if (sum == Todec(num, i))
{
index = i;
break;
}

return index;
}
int main()
{

char num1[11] ,num2[11];
int radix;
int f
9c08
lag;
long long int i;
long long int result ;
scanf( "%s", num1);
scanf( "%s", num2);
scanf( "%d", &flag);
scanf( "%d", &radix);
if (flag == 1)
{
i = Todec(num1, radix);
result = Findradix(num2, i);
}
else
{
i = Todec(num2, radix);
result = Findradix(num1, i);
}
if (result == -1)
printf("Impossible");
else if (result < 2)
printf("2");
else
printf("%d", result);

return 0;

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