// Run time: 0.010
#include <stdio.h>
#include <string.h>
int find(int *num, int len);
int calculate(int *num, int len, int base);
int main()
{
char str1[100], str2[100];
int arr1[100], arr2[100];
int len1, len2;
int i; // base1
int j; // base2
int x, y;
int m1, m2;
while (scanf("%s%s", str1, str2) == 2)
{
len1 = strlen(str1);
len2 = strlen(str2);
for ( i = 0; i < len1; i++)
{
if (str1[i] >= '0' && str1[i] <= '9')
arr1[i] = int(str1[i])-48;
else
arr1[i] = str1[i]- 'A' + 10;
}
for ( i = 0; i < len2; i++)
{
if (str2[i] >= '0' && str2[i] <= '9')
arr2[i] = int(str2[i])-48;
else
arr2[i] = str2[i] - 'A' + 10;
}
m1 = find(arr1, len1);
m2 = find(arr2, len2);
// printf("m1: %d, m2: %d\n", m1, m2);
bool state = false;
for ( i = m1; i <= 36; i++)
{
for ( j = m2; j <= 36; j++)
{
x = calculate(arr1, len1, i);
y = calculate(arr2, len2, j);
if (x < y)
break;
else
if (x == y)
{
state = true;
break;
}
}
if (state)
break;
}
if (!state) // no one can equal
printf("%s is not equal to %s in any base 2..36\n", str1, str2);
else
printf("%s (base %d) = %s (base %d)\n", str1, i, str2, j);
}
return 0;
}
int find(int *num, int len)
{
if (len == 1 && (num[0] == 0 || num[0] == 1))
return 2;
int i;
int max = num[0];
for ( i = 1; i < len; i++)
{
if (num[i] > max)
max = num[i];
}
return max+1;
}
int calculate(int *num, int len, int base)
{
int i;
int sum = 0;
int weight = 1;
for ( i = len-1; i >= 0; i--)
{
sum += num[i]*weight;
weight *= base;
}
return sum;
}
2009年2月13日 星期五
Q343: What Base Is This ?
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言