
[Note that pow(x,y) returns x to the power y]
[cpp]
#include <iostream.h>
#include <string.h>
#include <math.h>
char* str,*str2;
char max;
int base_1,base_2,i,j,k,num_1,num_2;
void main()
{
str=new char[100];
str2=new char[100];
while (cin>>str>>str2)
{
max=0;
for (i=0;i<strlen(str);i++)
{
if (str>max)
max=str;
}
if (max>='A' && max<='Z')
base_1= int(max) - int('A') + 10;
else
{
base_1=int(max)-int('0');
}
base_1++;
max=0;
for (i=0;i<strlen(str2);i++)
{
if (str2>max)
max=str2;
}
if (max>='A' && max<='Z')
base_2= int(max) - int('A') + 10;
else
{
base_2=int(max)-int('0');
}
base_2++;
for (i=base_1;i<37;i++)
{
num_1=0;
for (k=strlen(str)-1;k>=0;k--)
{
if (str[k]>='A' && str[k]<='Z')
num_1+=pow(i,strlen(str)-1-k)*(int(str[k])-int('A') + 10);
else
num_1+=pow(i,strlen(str)-1-k)*(int(str[k])-int('0'));
}
for (j=base_2;j<37;j++)
{
num_2=0;
for (k=strlen(str2)-1;k>=0;k--)
{
if (str2[k]>='A' && str2[k]<='Z')
num_2+=pow(j,strlen(str2)-1-k)*(int(str2[k])-int('A') + 10);
else
num_2+=pow(j,strlen(str2)-1-k)*(int(str2[k])-int('0'));
}
if (num_1==num_2)
break;
}
if (num_1==num_2)
break;
}
if (num_1==num_2)
cout<<str<<" (base "<<i<<") = "<<str2<<" (base "<<j<<")\n";
else
cout<<str<<" is not equal to "<<str2<<" in any base 2..36\n";
}
}
[/cpp]