## 343(What Base is This?)

Moderator: Board moderators

Try the cases...

Input:
Code: Select all
`2E 2153 10N 1331 879D 31286 5V`

Output:
Code: Select all
`2E is not equal to 2 in any base 2..36153 is not equal to 10 in any base 2..36N is not equal to 1 in any base 2..36331 (base 6) = 87 (base 15)9D (base 21) = 312 (base 8)86 (base 25) = 5V (base 35)`

Hope these help.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm

### help me plz.....to solve 343

i don't find any reason to get wr,plz anybody help me to find the bug in this code.

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

unsigned long con(char * c,int n)
{
int i,m,n1;
unsigned long p=0;
m=strlen(c);
for(i=0;i<m;i++)
{ n1=48;
if(c[i]>='A'&&c[i]<='Z')n1=n1+7;
p=(c[i]-n1)*pow(n,m-i-1) +p;
}
return p;
}

void main()
{
long b,m,n,i,j,k,p;
unsigned long c1[38],d1[100];
char c[20],d[20];
//freopen("c:\\123.txt","w",stdout);
while(scanf("%s %s",c,d)==2)
{
if(strcmp(c,"0")==0&&strcmp(d,"0")==0)
{
printf("%s (base 2) = %s (base 2)\n",c,d);
continue;
}

m=strlen(c);
b=0;
for(i=0;i<m;i++)
{n=48;
if(c[i]>='A'&&c[i]<='Z')n=n+7;
if(b<c[i]-n)b=c[i]-n;
}
k=b+1;

m=strlen(d);
b=0;
for(i=0;i<m;i++)
{ n=48;
if(d[i]>='A'&&d[i]<='Z')n=n+7;
if(b<d[i]-n)b=d[i]-n;
}
b=b+1;
for(i=k;i<=36;i++)
c1[i]=con(c,i);
for(i=b;i<=36;i++)
d1[i]=con(d,i);

p=0;
for(i=k;i<=36;i++)
{
for(j=b;j<=36;j++)
{
if(c1[i]==d1[j]){m=i;n=j;p=1;break;}

}
if(p==1)break;
}
if(p==1)
printf("%s (base %ld) = %s (base %ld)\n",c,m,d,n);
else printf("%s is not equal to %s in any base 2..36\n",c,d);
}
}
chinmoy kanti dhar
New poster

Posts: 19
Joined: Fri Jun 22, 2007 6:17 pm

Somebody help me!
I've tried all the test cases I could find on the board but still WA.
Could someone give me more critical test cases?

Code: Select all
`deleted`

By the way is there any negetive numbers in the input or some test case like this?
Code: Select all
`000 00000`
Last edited by WingletE on Thu Nov 15, 2007 1:57 pm, edited 1 time in total.

WingletE
New poster

Posts: 35
Joined: Sun Aug 13, 2006 1:34 pm
Location: Taipei, Taiwan

Try the cases.

Input:
Code: Select all
`9 21B 146 20`

Output:
Code: Select all
`9 (base 10) = 21 (base 4)B (base 12) = 14 (base 7)6 (base 7) = 20 (base 3)`

Btw I think the case you provided will not be given as input.

Hope these help.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm

Thanks. I got Accepted. =)

WingletE
New poster

Posts: 35
Joined: Sun Aug 13, 2006 1:34 pm
Location: Taipei, Taiwan

### Re: 343(What Base is This?)

plz help donno why my code gives WA.Checked all the posts nd got expected answers.
Code: Select all
`#include<iostream>using namespace std;long long pow(int i,int j){    long long val=1;    int k;    for (k=1;k<=j;k++)        val=val*i;    return val;}int main(){    char X[1000],Y[1000];    int l1,l2,maxx,maxy,i,j,k,FLAG;    long long N1,N2;    while (scanf("%s%s",X,Y)!=EOF)    {          l1=strlen(X);          l2=strlen(Y);          maxx=1,maxy=1;          for (i=0;i<l1;i++)              if (isdigit(X[i]))              {                 if (X[i]-48>maxx)                    maxx=X[i]-48;              }              else if (isalpha(X[i]))              {                  if (X[i]-55>maxx)                     maxx=X[i]-55;              }          for (i=0;i<l2;i++)              if (isdigit(Y[i]))              {                 if (Y[i]-48>maxy)                    maxy=Y[i]-48;              }              else if (isalpha(Y[i]))              {                  if (Y[i]-55>maxy)                     maxy=Y[i]-55;              }          maxx++;maxy++;          FLAG=0;          for (i=maxx;i<36 && FLAG==0;i++)              for (j=maxy;j<36 && FLAG==0;j++)              {                  N1=0;N2=0;                  for (k=l1-1;k>=0;k--)                      if (isdigit(X[k]))                         N1+=pow(i,l1-k-1)*(X[k]-48);                      else if (isalpha(X[k]))                           N1+=pow(i,l1-k-1)*(X[k]-55);                  for (k=l2-1;k>=0;k--)                      if (isdigit(Y[k]))                         N2+=pow(j,l2-k-1)*(Y[k]-48);                      else if (isalpha(Y[k]))                           N2+=pow(j,l2-k-1)*(Y[k]-55);                  if (N1==N2)                     FLAG=1;              }          if (FLAG==1)             printf("%s (base %d) = %s (base %d)\n",X,--i,Y,--j);          else              printf("%s is not equal to %s in any base 2..36\n",X,Y);    }}`
fR0D
New poster

Posts: 29
Joined: Mon Feb 11, 2008 5:59 am

### Re: 343(What Base is This?)

I have passed all the test case. I really don't know why I got a WA.

Code: Select all
`#include<stdio.h>#include<ctype.h>char v[2][20];int base[2];int tonum(char c){   if(isdigit(c))      return c-'0';   else      return toupper(c)-'A'+10;}long long translate(char *s, int base){   long long val = 0;   int t = 0;   while(*s){      val *= base;      t = tonum(*s);      if(t>=base)         return -1;      val += t;      s++;   }   return val;}int check(){   int i;   int j;   long long t[2][37];   for(i=0;i<2;i++)      for(j=2;j<=36;j++)         t[i][j]=translate(v[i],j);   for(i=2;i<=36;i++)      for(j=2;j<=36;j++)         if(t[0][i]==t[1][j]&&t[0][i]!=-1){            base[0]=i;            base[1]=j;            return 1;         }   return 0;}int main(){   while(scanf("%s%s",v[0],v[1])!=EOF){      if(check())         printf("%s (base %d) = %s (base %d)\n",               v[0],base[0],v[1],base[1]);      else         printf("%s is not equal to %s in any base 2..36\n",               v[0],v[1]);   }   return 0;}`
tython
New poster

Posts: 2
Joined: Tue Apr 22, 2008 5:23 pm

### Re: 343(What Base is This?)

AC
tython
New poster

Posts: 2
Joined: Tue Apr 22, 2008 5:23 pm

### Re: 343(What Base is This?)

I cant understand why i m getting WA pls help
Code: Select all
`#include<stdio.h>#include<string.h>#include<ctype.h>#include<math.h>char num1[50],num2[50];long long value1,value2;long long basevalue(char num[],int base){   int i,j,length;   long long value=0;   length=strlen(num);   for(i=length-1,j=0;i>=0;i--,j++)   {      if(isalpha(num[i]))         value=value+int(num[i]-55)*(long long)( pow(double(base),double(j)) );      else         value=value+int(num[i]-48)*(long long)(pow(double(base),double(j)));   }   return value;}int main(){       int len1,len2,i,j,flag=0,high1,high2;   while( (scanf("%s %s",&num1,&num2))==2 )   {      len1=strlen(num1);      len2=strlen(num2);      high1=0;      high2=0;      if(num1[0]=='0' && num2[0]=='0')      {         printf("0 (base 2) = 0 (base 2)\n");          continue;      }      for(i=0;i<len1;i++)      {         if(isdigit(num1[i]))         {            if(high1<num1[i]-48)               high1=num1[i]-48;         }         else         {            if(num1[i]-55>high1)               high1=num1[i]-55;         }      }      for(i=0;i<len2;i++)      {         if(isdigit(num2[i]))         {            if(high2<num2[i]-48)               high2=num2[i]-48;         }         else         {            if(num2[i]-55>high2)               high2=num2[i]-55;         }      }      for(i=2;i<=36 && !flag;i++)      {            if(i<=high1)            continue;         value1=basevalue(num1,i);         for(j=2;j<=36;j++)         {            if(j<=high2 )               continue;            value2=basevalue(num2,j);            if(value1==value2){               flag=1;               break;            }         }      }      if(flag)         printf("%s (base %d) = %s (base %d)\n",num1,i-1,num2,j);      else         printf("%s is not eqal to %s in any base 2..36\n",num1,num2);      flag=0;   }   return 0;}`
minor_coder
New poster

Posts: 1
Joined: Tue Sep 15, 2009 9:46 am

### Re: 343(What Base is This?)

You should try to avoid built_in pow() function for this type of problem. It may cause some troubles. . .I also got a WA and when I removed the pow() function, I got accepted
Hope it helps
Taman
New poster

Posts: 32
Joined: Sun Oct 11, 2009 8:59 pm
Location: Southeast University

### Re: 343(What Base is This?)

I'ev encounted a weird situation, all the logic seems no problem, and at first I implement the pair match as follows
Code: Select all
`      while(start_index_one < 37 && start_index_one < 37)      {      if(value_one[start_index_one] < value_two[start_index_two])         start_index_one++;      else if(value_one[start_index_one] > value_two[start_index_two])         start_index_two++;      else      {         index_one = start_index_one;         index_two = start_index_two;         return true;`

cos I thought that the value should increase as the base increses, and the problem you shou solve is just to find the smallest pair you could in two increasing series, but I always got WA, and then I looked at sb else's code and changed it to
Code: Select all
`for ( int i = start_index_one; i <= 36; i++ ) {      for ( int j = start_index_two; j <= 36; j++ ) {         if ( value_one[i] == value_two[j] ) {            index_one = i;            index_two = j;            return true;         }      }   }`

this seems more complete in logic, but I don't think there's any difference in those two on this situation, and I've done thousands of tests using random-generated input, and those two eactly give the same result, any body please tell me what I have missed here? If I didn't get it wrong, my person understanding is that there must be overflow here, the value is not an increasing series
jobbysunrise
New poster

Posts: 1
Joined: Sat Jun 09, 2007 11:41 am

### Re: 343(What Base is This?)

import java.util.Scanner;

/**
*
* @author mostafa
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input=new Scanner(System.in);
while(input.hasNext())
{
String first=input.next();
String second=input.next();
int [] firstDigits=StringToIntArray(first);
int [] secondDigits=StringToIntArray(second);
int FirstMinIndex=Math.max(MaxNum(firstDigits)+1,2);
int SecondMinIndex=Math.max(MaxNum(secondDigits)+1,2);
int firstindex=0;
int secondindex=0;
boolean stop=false;
for(int i=FirstMinIndex;i<=36&&!stop;i++)
for(int j=SecondMinIndex;j<=36&&!stop;j++)
{
if(getTheNumber(firstDigits, i)==getTheNumber(secondDigits, j))
{
firstindex=i;
secondindex=j;
stop=true;
break;
}
}
if(firstindex!=0)
{
System.out.println(first+" "+"(base "+firstindex+") = "+second+" (base "+secondindex+")");
}
else
{
System.out.println(first+" is not equal to "+second+" in any base 2..36");
}
}
}

private static int[] StringToIntArray(String first) {
int [] array=new int[first.length()];
for(int i=0;i<first.length();i++)
{
if(first.charAt(i)<='9')
array[i]=(int)first.charAt(i)-48;
else
array[i]=(int)first.charAt(i)-55;
}
return array;
}

private static int MaxNum(int[] Array) {
int max=0;
for(int i=0;i<Array.length;i++)
{
max=Math.max(max, Array[i]);
}
return max;
}

private static int getTheNumber(int[] firstDigits, int base) {
int number=0;
for(int i=firstDigits.length-1;i>=0;i--)
{
number+=firstDigits[i]*Math.pow(base, firstDigits.length-i-1);
}
return number;
}

}

my code pass all the test cases on this post and i still got Wrong Answer
plz help me
sasa_blood
New poster

Posts: 1
Joined: Sat Feb 19, 2011 2:43 pm

### Re: 343(What Base is This?)

#include<stdio.h>
#include<math.h>
#include<string.h>

int findMinBase(char x[])
{
int mb=2,b,i;
for(i=0;x[i];i++)
{

if(x[i]>=65)
b=x[i]-54;
else b=x[i]-47;
if(b>mb)mb=b;
}
return mb;
}
int toDec(char x[],int base)
{
int i=strlen(x)-1;
int sum=0,j=0,b;
for(;i>=0;i--)
{
if(x[i]>=65)
b=x[i]-55;
else b=x[i]-48;
sum+=b*pow(base,j++);
}
return sum;
}
main()
{
char x[50],y[50],z[1000];
int mbx,mby,i,j,s,dx,dy;

while(gets(z))
{
sscanf(z,"%s%s",x,y);
mbx=findMinBase(x);
mby=findMinBase(y);
s=0;
for(i=mbx;i<37;i++)
{
dx=toDec(x,i);
for(j=mby;j<37;j++)
{

dy=toDec(y,j);
if(dx==dy)
{
printf("%s (base %d) = %s (base %d)\n",x,i,y,j);
s=1;break;

}
}
if(dx==dy)break;
}
if(s==0)printf("%s is not equal to %s in any base 2..36\n",x,y);

}

return 0;
}
shiv
New poster

Posts: 2
Joined: Mon May 02, 2011 9:53 pm

### Re: 343(What Base is This?)

@jan Thank you sir, i got Ac by your provided Cases.Thank you.
live_lie
New poster

Posts: 19
Joined: Mon Nov 29, 2010 11:50 pm

### Re: 343(What Base is This?)

I've tried all the inputs I found but still received WA.

Sample Input
Code: Select all
`12 510 A12 34123 4561 210 20 010 3635 ZZ 351 12 23 34 45 56 67 78 87FR82OGF 7FR82OGFIGW6NFP IGW6NFPI7U4NK1 I7U4NK14NTD52X 4NTD52XNMEP0AI NMEP0AIID9QJFE ID9QJFEIU0TLAO IU0TLAO4CY20XP 4CY20XP2U6OA34 2U6OA342E 2153 10N 1331 879D 31286 5V9 21B 146 20`

Sample Output (= My Output)
(comfirmed by UVA toolkit: http://uvatoolkit.com/problemssolve.php, they are correct!)
Code: Select all
`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..361 is not equal to 2 in any base 2..3610 (base 2) = 2 (base 3)0 (base 2) = 0 (base 2)10 (base 27) = 36 (base 7)35 (base 10) = Z (base 36)Z (base 36) = 35 (base 10)1 (base 2) = 1 (base 2)2 (base 3) = 2 (base 3)3 (base 4) = 3 (base 4)4 (base 5) = 4 (base 5)5 (base 6) = 5 (base 6)6 (base 7) = 6 (base 7)7 (base 8) = 7 (base 8)8 (base 9) = 8 (base 9)7FR82OGF (base 28) = 7FR82OGF (base 28)IGW6NFP (base 33) = IGW6NFP (base 33)I7U4NK1 (base 31) = I7U4NK1 (base 31)4NTD52X (base 34) = 4NTD52X (base 34)NMEP0AI (base 26) = NMEP0AI (base 26)ID9QJFE (base 27) = ID9QJFE (base 27)IU0TLAO (base 31) = IU0TLAO (base 31)4CY20XP (base 35) = 4CY20XP (base 35)2U6OA34 (base 31) = 2U6OA34 (base 31)2E is not equal to 2 in any base 2..36153 is not equal to 10 in any base 2..36N is not equal to 1 in any base 2..36331 (base 6) = 87 (base 15)9D (base 21) = 312 (base 8)86 (base 25) = 5V (base 35)9 (base 10) = 21 (base 4)B (base 12) = 14 (base 7)6 (base 7) = 20 (base 3)`

Hope somebody can find out what mistake I make.

----
My arrays are too small to store the input.
Just change
Code: Select all
`  char input[2][30], processed_input[2][30];`

to
Code: Select all
`  char input[2][100], processed_input[2][100];`

and I got Accepted.
bill8124
New poster

Posts: 8
Joined: Fri Jan 21, 2011 8:13 am

Previous