## 343(What Base is This?)

All about problems in Volume III. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

### 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[i]>max)
max=str[i];
}
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[i]>max)
max=str2[i];
}
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]
mido
Learning poster

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

I'm also getting Wrong Answer!
Please give me some input...!

Thanks!
Please visit my webpage!! I've got a lot of UVA statistics scripts
http://www.fe.up.pt/~ei01081/scripts/
RuiFerreira
New poster

Posts: 23
Joined: Mon Dec 16, 2002 8:01 pm
Location: Portugal

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
mido
Learning poster

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

with input:
0 0
0 (base 1) = 0 (base 1)
but should be:
0 (base 2) = 0 (base 2)
afonsocsc
New poster

Posts: 34
Joined: Mon Mar 24, 2003 1:15 am
Location: Portugal, Lisbon

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.
Sneeze
New poster

Posts: 13
Joined: Thu Jan 30, 2003 4:04 am

sneeze, I ran your program with my input and here's the results...
Code: Select all
`input:12   5    10     A12 34  123   456  1    2  10   2  10 36  35 Z0 0your output: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)10 (base 27) = 36 (base 7)35 is not equal to Z in any base 2..36my 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..361 is not equal to 2 in any base 2..3610 (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.
afonsocsc
New poster

Posts: 34
Joined: Mon Mar 24, 2003 1:15 am
Location: Portugal, Lisbon

Thank you. I got AC.
I should have examined my code more carefully.
Sneeze
New poster

Posts: 13
Joined: Thu Jan 30, 2003 4:04 am

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
oriol78
New poster

Posts: 32
Joined: Mon Mar 31, 2003 7:39 pm

Base 2 (i.e. binary) numbers can't contain the digit 5, only 0's and 1's.
Per
A great helper

Posts: 429
Joined: Fri Nov 29, 2002 11:27 pm
Location: Sweden

### What's wrong with my code?

what's wrong with my code?
Code: Select all
`#include <stdio.h>#include <string.h>#include <math.h>char a[50];char a1[50];char b[50];char b1[50];main(){   int i,j,h,la,lb,f,ma,mb;   unsigned long va,vb;   while(scanf("%s%s",&a1,&b1)==2)   {      ma=mb=0;      strcpy(a,a1);      strcpy(b,b1);      f=0;      la=strlen(a)-1;      lb=strlen(b)-1;      for(i=0;i<=la;i++)      {         if(a[i]<='Z' && a[i]>='A')            a[i]-=7;         a[i]=a[i]-'0';         if(a[i]>ma)            ma=a[i];      }      for(i=0;i<=lb;i++)      {         if(b[i]<='Z' && b[i]>='A')            b[i]-=7;         b[i]=b[i]-'0';         if(b[i]>mb)            mb=b[i];      }      if(ma==0)         ma=1;      if(mb==0)         mb=1;      for(i=ma+1;i<=36;i++)      {         va=a[la];         for(h=la-1;h>=0;h--)         {            va+=a[h]*(int)pow(i,(la-h));         }         for(j=mb+1;j<=36;j++)         {            vb=b[lb];            for(h=lb-1;h>=0;h--)            {               vb+=b[h]*(int)pow(j,(lb-h));            }            if(i!=j && va==vb)            {               f=1;               break;            }         }         if(f)            break;      }      if(va==0 && vb==0)         printf("0 (base 2) = 0 (base 2)\n");      else      if(j==37 && i==37)         printf("%s is not equal to %s in any base 2..36\n",a1,b1);      else         printf("%s (base %d) = %s (base %d)\n",a1,i,b1,j);   }   return 0;}`
Ashkankhan
New poster

Posts: 12
Joined: Wed Oct 13, 2004 10:14 am
Location: Teh

I didn't found any worng with my code, but I am getting WA. Maybe I got WA more than 10 times. I checked it with all I/O from board but its giving the correct answer.

Now I am giving my code. Anybody who got AC can check it with his code.

If anybody help me than I would be grateful to him.

My code:
Code: Select all
`Remove after AC`
Last edited by Mushfiqur Rahman on Sat Mar 03, 2007 3:20 pm, edited 1 time in total.
Mushfiqur Rahman
Learning poster

Posts: 56
Joined: Tue Jun 13, 2006 5:18 pm
Location: (CSE, SUST) Sylhet, Bangladesh

### To Ahskankhan

Dear, Ashkankhan

Your code is giving wrong output with this following input.
Input:

Code: Select all
` 1   1  2    2   3     3   4  45  56 67 78 8`

Fix it and got AC
Mushfiqur Rahman
Learning poster

Posts: 56
Joined: Tue Jun 13, 2006 5:18 pm
Location: (CSE, SUST) Sylhet, Bangladesh

i'm really helpless. i've collected more than 10 WA with this one. anyone to help me ...???
Code: Select all
`/*   What Base Is This?*/#include <stdio.h>#include <string.h>#include <ctype.h>#include <math.h>#define max 300char a1[max], a2[max];long todeci(char arr[], int base){   int i, len = strlen(arr);   long d = 0, dum;   for(i = 0; i < len; i++)   {      if(isdigit(arr[i]))         dum = arr[i] - '0';      if(isalpha(arr[i]))         dum = arr[i] - 'A' + 10;      d += (long)pow(base, (len - i - 1)) * (dum);   }   return d;}int startbase(char s[]){   int i, base = 2, len = strlen(s), dum;   for(i = 0; i < len; i++)   {      if(isdigit(s[i]))         dum = s[i] - '0';      else if(isalpha(s[i]))         dum = s[i] - 'A' + 10;      if(dum >= base)         base = dum + 1;   }   return base;}char *rmvzero(char a[], char b[]){   int len = strlen(b), k, l;      strcpy(a, b);   for(k = 0; k < len; k++)   {      if(a[k] != '0' || len == 1)         break;      else      {         for(l = k; a[l]; l++)            a[l] = a[l + 1];                  k--;         len--;      }   }   return a;}int main(){   char s[max], s1[max], s2[max];   int i, j, b1, b2, flag, sb1, sb2;   long n1, n2, min1, min2;   while(gets(s))   {      if(strlen(s) == 0)         continue;      sscanf(s,"%s %s", s1, s2);      n1 = n2 = -1;      min1 = min2 = flag = 0;      rmvzero(a1, s1);      rmvzero(a2, s2);      sb1 = startbase(a1);      sb2 = startbase(a2);      for(i = sb1; i <= 36; i++)      {         for(j = sb2; j <= 36; j++)         {            if(!min1)            {               n1 = todeci(s1, i);               min1 = 1;               b1 = i;            }                        if(!min2)            {               n2 = todeci(s2, j);               min2 = 1;               b2 = j;            }            if( n1 > -1 &&  n2 > -1)            {               if(n1 == n2)                  flag = 1;               else                  min1 = min2 = 0;            }            if(flag)               break;            }         if(flag)            break;      }      if(flag)         printf("%s (base %d) = %s (base %d)\n", s1, b1, s2, b2);      else         printf("%s is not equal to %s in any base 2..36\n", s1, s2);   }   return 0;}`

kana
New poster

Posts: 19
Joined: Mon Mar 13, 2006 6:03 pm
Location: dhaka

Try the cases...

Input:
Code: Select all
`7FR82OGF   7FR82OGF IGW6NFP   IGW6NFP I7U4NK1   I7U4NK1 4NTD52X   4NTD52X NMEP0AI   NMEP0AI ID9QJFE   ID9QJFE IU0TLAO   IU0TLAO 4CY20XP   4CY20XP 2U6OA34   2U6OA34 `

Output:
Code: Select all
`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)`

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

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

### My code for 343 is getting WA

My code is able to give correct answers for the following test cases but still gets WA

Code: Select all
`ERASED`

Thanks a bunch! Jan my code got AC
Last edited by mrunmoy on Fri Apr 13, 2007 11:12 am, edited 1 time in total.
Best Regards,
Mrunmoy.

I belong to { IDIOTS }
IDIOTS - Intelligent Dynamic & Innovative On-The-Spot!

mrunmoy
New poster

Posts: 17
Joined: Mon Apr 09, 2007 3:11 pm
Location: India

Next