## 343 - What Base Is This?

Moderator: Board moderators

mido
Learning poster
Posts: 78
Joined: Sun Jun 16, 2002 9:48 pm
Location: Cairo,Egypt

### 343 - What Base Is This?

This gets me a WA...any tips :
[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]
RuiFerreira
New poster
Posts: 23
Joined: Mon Dec 16, 2002 8:01 pm
Location: Portugal
Contact:

Thanks!
Please visit my webpage!! I've got a lot of UVA statistics scripts
http://www.fe.up.pt/~ei01081/scripts/
mido
Learning poster
Posts: 78
Joined: Sun Jun 16, 2002 9:48 pm
Location: Cairo,Egypt
I've got AC...here'e some tips :

.Minimum base is 2
.while there are still digits
result= result* current testing base + value of digit
.Go to http://www.comp.nus.edu.sg/~stevenha/

Hope it helps
afonsocsc
New poster
Posts: 34
Joined: Mon Mar 24, 2003 1:15 am
Location: Portugal, Lisbon
with input:
0 0
0 (base 1) = 0 (base 1)
but should be:
0 (base 2) = 0 (base 2)
Sneeze
New poster
Posts: 13
Joined: Thu Jan 30, 2003 4:04 am
My code is:

#include <iostream>
using namespace std;

int FindMinBase(char *number)
{
int max=0, temp;
char *current=number;
while(*current!='\0')
{
if(*current>='0' && *current<='9')
temp=(int)(*current-'0');
else
temp=10+(int)(*current-'A');
if(temp>max)
max=temp;
current++;
}
if(max==0)
return 2;
return (max+1);
}

unsigned long GetVal(char *number, int base)
{
unsigned long temp=0;
char *current=number;
while(*current!='\0')
{
temp*=base;
if(*current>='0' && *current<='9')
temp+=(unsigned long)(*current-'0');
else
temp+=10+(unsigned long)(*current-'A');
current++;
}
return (temp);
}

int main()
{
char num1[100], num2[100];
int base1, base2;
bool tocontinue;

cin >> num1 >> num2;
while(cin.good())
{
tocontinue=true;
for(base1=FindMinBase(num1); base1<=32 && tocontinue; base1++)
for(base2=FindMinBase(num2); base2<=32; base2++)
if(GetVal(num1, base1)==GetVal(num2, base2))
{
cout << num1 << " (base " << base1 << ") = ";
cout << num2 << " (base " << base2 << ")" << endl;
tocontinue=false;
break;
}
if(tocontinue)
cout << num1 << " is not equal to "
<< num2 << " in any base 2..36" << endl;
cin >> num1 >> num2;
}
return 0;
}

I wonder why I got WA.
afonsocsc
New poster
Posts: 34
Joined: Mon Mar 24, 2003 1:15 am
Location: Portugal, Lisbon
sneeze, I ran your program with my input and here's the results...

Code: Select all

``````input:
12   5
10     A
12 34
123   456
1    2
10   2
10 36
35 Z
0 0

12 (base 3) = 5 (base 6)
10 (base 10) = A (base 11)
12 (base 17) = 34 (base 5)
123 is not equal to 456 in any base 2..36
1 is not equal to 2 in any base 2..36
10 (base 2) = 2 (base 3)
10 (base 27) = 36 (base 7)
35 is not equal to Z in any base 2..36

my output (AC):
12 (base 3) = 5 (base 6)
10 (base 10) = A (base 11)
12 (base 17) = 34 (base 5)
123 is not equal to 456 in any base 2..36
1 is not equal to 2 in any base 2..36
10 (base 2) = 2 (base 3)
10 (base 27) = 36 (base 7)
35 (base 10) = Z (base 36)
0 (base 2) = 0 (base 2)
``````
Hope it helps...
Last edited by afonsocsc on Tue Jun 03, 2003 10:48 am, edited 1 time in total.
Sneeze
New poster
Posts: 13
Joined: Thu Jan 30, 2003 4:04 am
Thank you. I got AC.
I should have examined my code more carefully.
oriol78
New poster
Posts: 32
Joined: Mon Mar 31, 2003 7:39 pm
sorry, my english is poor and i don't understand any question...
for 12 5
why the output is 3 and 6
3 and 2 is not correct?
1*(3^1) + 2*(3^0) = 5
5*(2^0) = 5 too
can anyboy explain me plz? thx
Per
A great helper
Posts: 429
Joined: Fri Nov 29, 2002 11:27 pm
Location: Sweden
Base 2 (i.e. binary) numbers can't contain the digit 5, only 0's and 1's.
Azim
New poster
Posts: 2
Joined: Fri Mar 18, 2005 12:05 am
Location: PL

### 343 What Base Is This? Help

Hi all,

I'm really tired and enough of this problem... It's an easy one, but still WA!! I tasted my program for all inputs on forum and all makes good out. If anyone can help me?

Code: Select all

``````const
b = 10;
var
a,i,j,n,m,q,f,h:longint;
x,y,k,l:string;
k2,l2:longint;
c:boolean;
r:char;

procedure zmia_sys;
begin
q:=1; n:=0;

for m:=length(x) downto 1 do
begin
if (ord(x[m])>=65) then n:=n+(q*(ord(x[m])-55)) else n:=n+(q*(ord(x[m])-48));
q:=q*a;
end;

end;

begin

while not eof do
begin

k:=''; l:=''; c:=false;
while l='' do
begin

while not eoln do
begin
if (r=' ') and (k<>'') then c:=true else
if (r<>' ') and (c=false) then k:=k+r else
if (r<>' ') then l:=l+r;

if k<>'' then c:=true;
end;

i:=1;
for m:=1 to length(k) do
if (ord(k[m])>=65) and (ord(k[m])-55 > i) then i:=ord(k[m])-55 else
if (ord(k[m])-48 > i) then i:=ord(k[m])-48;

j:=1;
for m:=1 to length(l) do
if (ord(l[m])>=65) and (ord(l[m])-55 > j) then j:=ord(l[m])-55 else
if (ord(l[m])-48 > j) then j:=ord(l[m])-48;

c:=false;
for f:=(i+1) to 36 do
begin
x:=k; a:=f; zmia_sys; k2:=n;

for h:=(j+1) to 36 do
begin
x:=l; a:=h; zmia_sys; l2:=n;

if (k2=l2) then
begin
c:=true;
break;
end;
end;

if c=true then break;
end;

if c=true then writeln(k,' (base ',f,') = ',l,' (base ',h,')','    ',y)
else writeln(k,' is not equal to ',l,' in any base 2..36');
end;

end.``````
Thx,
Azim[/code]
mmij
New poster
Posts: 10
Joined: Mon Jul 11, 2005 7:13 am
Location: PlanetEarth

### 343-what base is this? why WA???

but getting WA?? plz hlp

#include<stdio.h>
#include<stdlib.h>

int find_max(char *str)
{
int i;
char max;
max = '0';
for(i=0; str; i++)
{
if(max<str)
max = str;
}

if(max>='0' && max<='9')
return max-'0'+1;
else
return max-'A'+11;
}

int main()
{
int i, j, flag, max1, max2;
long arr1[37], arr2[37];
char str1[100], str2[100];
char *s;

while(scanf("%s %s", str1, str2) == 2)
{
flag = 0;

max1 = find_max(str1);
max2 = find_max(str2);

for(i=max1; i<=36; i++)
arr1 = strtol(str1, &s, i);

for(j=max2; j<=36; j++)
arr2[j] = strtol(str2, &s, j);

if(max1==1 && max2==1)
{
printf("%s (base 2) = %s (base 2)\n", str1, str2);
continue;
}

for(i=max1; i<=36; i++)
{
if(arr1 == 0)
continue;
for(j=max2; j<=36; j++)
{
if(arr2[j] == arr1)
{
flag = 1;
break;
}
}
if(flag)
{
printf("%s (base %d) = %s (base %d)\n", str1, i, str2, j);
break;
}
}

if(!flag)
printf("%s is not equal to %s in any base 2..36\n", str1, str2);
}

return 0;
}[/code]
murkho
New poster
Posts: 33
Joined: Mon Mar 28, 2005 6:41 pm

### 343>>Can U tell me why WA??

Can u give me some input for which WA ? or where i am wrong in this code?
[/code]
#include<stdio.h>
#include<string.h>
#include<math.h>

char num1[20],num2[20];

{
int i,j,l1;
l1 = strlen(p)-1;
for(i = 0;i<l1;i++)
{
if(p[0]=='0')
p++;
else
break;
}
strcpy(num1,p);
l1 = strlen(q) -1;
for(i = 0;i<l1;i++)
{
if(q[0]=='0')
q++;
else
break;
}
strcpy(num2,q);

}

void strrevd(char *p)
{
int i= 0 ,len,j;
char ch;
len = strlen(p) -1;
j = len;
while(i<=len/2 && j>=len/2)
{
ch = *(p+i);
*(p+i) = * (p +j);
*(p+j) = ch;
i++;
j--;
}

}

int base(char ch)
{
if(ch >='0' && ch<='9')
return ch - 48+1;
else if(ch >= 'A' && ch <= 'Z')
return ch - 55 + 1;

}

int minimum_base(char p[])
{
int i,bs = 2;
for(i = 0;p;i++)
{
if(base(p) >bs)
bs = base(p);

}

return bs;
}

long equal_val(char p[],int bs)
{
int i,j,t;
char tmp[100];
unsigned long res;
strcpy(tmp,p);
strrevd(tmp);
res =0; //!!!
for(i = 0;tmp;i++)
{
t = base(tmp)-1;
res += pow(bs,i)* t;

}
return res;

}

int main()
{

unsigned long a,b,i,j;
unsigned long ba,bb;
int flag = 0;
char str[1000],*p;

//freopen("343.in","r",stdin);
while(gets(str)!= NULL)
{
flag = 0;
p = strtok(str,"\t ");
strcpy(num1,p);
p = strtok(NULL,"\t ");
strcpy(num2,p);
ba = minimum_base(num1);
bb = minimum_base(num2);
for(i = ba;i<=36;i++)
{
for(j = bb;j<=36;j++)
{
a = equal_val(num1,i);
b = equal_val(num2,j);
if(a == b)
{

printf("%s (base %lu) = %s (base %lu)\n",num1,i,num2,j);
flag = 1;
break;
}
if(b>a)
break;
}
if(flag ==1)
break;
}

if(flag ==0)
printf("%s is not equal to %s in any base 2..36\n",num1,num2);

}

return 0;
}

asr
New poster
Posts: 3
Joined: Tue Sep 20, 2005 8:11 am
Location: IIIT-H

### 343: What Base is this ::WA: Explanation of logic enclosed

Hello,
This is my first ACM prog.
This is what I took care:-
1. Long Integers (used unsigned long long).
2. Took care of Minimum base of a number.
3. Took help again from some of others I knew , in vain though.
Below is the code:-

Code: Select all

``````#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned long long base1,base2;
/*For a given number stored in char*n, of base it converts to its equivalent in base 10 */
unsigned long  convBase10 ( char *  n , int base ){
unsigned long long val=0;
unsigned long long mult=1;
int i;
for( i=strlen(n)-1 ; i>=0 ; i--){
if ( n[i] >= '0' && n[i] <= '9' )
val += (n[i]-'0')*mult;
else if ( n[i] >= 'A' && n[i] <= 'Z' )
val += (n[i]-'A' + 10) *mult;
mult *= base;
}
return val;
}
int getMinBase(char *n){
int i;
usigned int base=2;
for( i=strlen(n)-1 ; i>=0 ; i--){
if ( n[i] >= '0' && n[i] <= '9' )
base = ( n[i]-'0' >= base ) ? n[i] -'0' + 1 : base ;
else if ( n[i] >= 'A' && n[i] <= 'Z' )
base = ( (n[i]-'A' + 10 ) >= base ) ? n[i] -'0' + 1 : base ;
}
return base ;
}
int isSame(char * num1 , char * num2){
unsigned int i=0,j=0;
for( i = getMinBase(num1) ; i <= 36 ;i++)
for ( j = getMinBase(num2) ; j<=36 ;j++)
if ((convBase10(num1,i)) == (  convBase10( num2,j ))){
base1=i;base2=j;return 1;
}
return 0;
}

main(){
char n1[30],n2[30];
while (!feof(stdin)){
scanf(" %[0-9A-Z]",n1);
scanf(" %[0-9A-Z]",n2);
if( feof (stdin))
break;
if ( isSame(n1,n2) )
printf("%s (base %lld) = %s (base %lld)\n",n1,base1,n2,base2);
else
printf("%s is not equal to %s in any base 2..36\n",n1,n2);
}
}``````
asr
New poster
Posts: 3
Joined: Tue Sep 20, 2005 8:11 am
Location: IIIT-H
Sorry, Sillly mistake, i got AC
h001122
New poster
Posts: 9
Joined: Thu Feb 24, 2005 5:59 am

Code: Select all

``````Accepted
``````
Sample inputs :

Code: Select all

``````12   5
10     A
12 34
123   456
1    2
10   2
``````
I tried input sample inputs into my program, and it outputed correct answer in that inputs.
But I got WA.
I really don't know what's wrong.