## 355 - The Bases Are Loaded

Moderator: Board moderators

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
Try the cases...

Input:

Code: Select all

``````10 16 58
10 5 44
10 2 62342
10 13 1``````
Output:

Code: Select all

``````58 base 10 = 3A base 16
44 base 10 = 134 base 5
62342 base 10 = 1111001110000110 base 2
1 base 10 = 1 base 13``````
Hope these help.
Ami ekhono shopno dekhi...
HomePage

jainal cse du
New poster
Posts: 23
Joined: Thu Jul 27, 2006 2:43 pm
removed
Last edited by jainal cse du on Sun Mar 18, 2007 2:23 pm, edited 1 time in total.

ishtiaq ahmed
Learning poster
Posts: 53
Joined: Sat Jul 29, 2006 7:33 am

### dear Jainal

I have tried this input as follows:

Code: Select all

``````2 2 0001
``````
according to your code the output is

Code: Select all

``````0001 base 2 = 0001 base 2
0001 base 2 = 110010000000101000011101010001 base 2
``````
but correct output is

Code: Select all

``````1 base 2 = 1 base 2
``````
Best of luck
No venture no gain

with best regards
------------------------
ishtiaq ahmed

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
jainal cse du wrote:jan bhai,
I have changed my code as following and it gives correct result for these inputs . but I am still getting WA.
Try the cases...

Input:

Code: Select all

``````14 14 5A144D49
16 16 4``````
Output:

Code: Select all

``````5A144D49 base 14 = 5A144D49 base 14
4 base 16 = 4 base 16``````
'pow' function returns 'double', not integer. So, it can make precision errors. And you can easily get rid of it.
Replace

Code: Select all

``````   for(i = 0; str[i]; i++)
{
if(str[i] > '9')
digit = str[i] - 55;
else
digit =  str[i] - 48;
num += digit * pow(f_base,power);
power--;
}``````

with

Code: Select all

``````   for(i = 0; str[i]; i++)
{
if(str[i] > '9')
digit = str[i] - 55;
else
digit =  str[i] - 48;
num=num*f_base+digit;
}``````
Hope these help. And don't forget to remove your previous code.
ishtiaq ahmed wrote:I have tried this input as follows:

Code: Select all

``````2 2 0001
``````
I think it is not a valid case. Because my code returns

Code: Select all

``0001 base 2 = 1 base 2``
Ami ekhono shopno dekhi...
HomePage

jainal cse du
New poster
Posts: 23
Joined: Thu Jul 27, 2006 2:43 pm
jan bhai,
I have updated my code again,gives correct output for all the inputs you have given. But still getting WA.

Code: Select all

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

long long in_base_10(long long f_base,char *str);
void in_require_base(long long base10,long long to_base);
char str[10000];
long long f_base;

int main()
{
long long to_base,digit,base10,i,n;

while( scanf("%lld %lld %s",&f_base,&to_base,str) != EOF)
{
int flag = 1;
for(i = 0; str[i]; i++)
{
if(str[i] >= 'A' && str[i] <= 'F')
digit = str[i] - 55;
else if('1'>= str[i] && str[i] <= '9')
digit = str[i] - 48;
if(!(digit <= f_base - 1))
{
printf("%s is an illegal base %lld number\n",str,f_base);
flag = 0;
break;
}
}

if(flag)
{
if(f_base != 10)
base10 = in_base_10(f_base,str);
else
base10 = atol(str);
in_require_base(base10,to_base);
}
}
return 0;
}
long long in_base_10(long long f_base,char *str)
{
long long i,digit;
long long num = 0;
for(i = 0; str[i]; i++)
{
if(str[i] > '9')
digit = str[i] - 55;
else
digit =  str[i] - 48;
num = num * f_base + digit;
}
return num;
}
void in_require_base(long long base10,long long to_base)
{
long long  i = 0,j, mode,temp,len;
char a[10000];
while(base10)
{
mode = base10 % to_base;
if(mode > 9)
a[i++] = mode + 55;
else
a[i++] = mode + 48;
base10 /= to_base;
}
a[i] = '\0';
len = strlen(a);
for(i = 0,j = len -1; i < j; i++,j--)
{
temp = a[i];      /* for string reverse*/
a[i] = a[j];
a[j] = temp;
}
if(!len)
a[len] = '0';
a[len + 1] ='\0';
printf("%s base %lld = %s base %lld\n",str,f_base,a,to_base);

}
``````

while(1)
{
printf("Thanks\n");
}

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
Replace the following unnecessary line

Code: Select all

``else if('1'>= str[i] && str[i] <= '9')``
with just

Code: Select all

``else``
Hope this works.
Ami ekhono shopno dekhi...
HomePage

mrunmoy
New poster
Posts: 17
Joined: Mon Apr 09, 2007 3:11 pm
Location: India
Contact:

### getting WA in 355

Dear Gurus,

Need your help in finding out the reason for WA.

Code: Select all

``ERASED``
i didnt take care about the power function for unsigned long long case.

It got AC after change.
Thanks a bunch!
Last edited by mrunmoy on Fri Apr 13, 2007 3:59 pm, edited 1 time in total.
Best Regards,
Mrunmoy.

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

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
Try

Code: Select all

``````16 10 1000000000
16 10 FEDCBA9876
``````
Correct outputs:

Code: Select all

``````1000000000 base 16 = 68719476736 base 10
FEDCBA9876 base 16 = 1094624909430 base 10
``````

mrunmoy
New poster
Posts: 17
Joined: Mon Apr 09, 2007 3:11 pm
Location: India
Contact:

### Thanks mf

Thanks to your test case mf.

Please provide some insights into testing my code with such test cases. how do you arrive at those test cases? so that going ahead i can test my codes rigorously before submitting...
Best Regards,
Mrunmoy.

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

New poster
Posts: 11
Joined: Thu Jun 15, 2006 5:46 pm
Location: Juchitán Oaxaca, México
Contact:

ishtiaq ahmed wrote:I have tried this input as follows:

Code: Select all

``````2 2 0001
``````
I think it is not a valid case. Because my code returns

Code: Select all

``0001 base 2 = 1 base 2``

I'm agree with you, both outputs are correct!

Code: Select all

``````0001 base 2 = 1 base 2
0001 base 2 = 0001 base 2
``````
Making simple things simple, making complex things possible!!

spider_6765
New poster
Posts: 9
Joined: Sun Jan 08, 2006 9:57 pm
can 'unsigned long long' can hold the value of 9999999999(decimal)?

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
'long long' uses 64 bit. So, it can store the given value easily. The highest positive value in 'long long' is 9223372036854775807.

Hope it helps.
Ami ekhono shopno dekhi...
HomePage

handsomepot
New poster
Posts: 5
Joined: Sun Jul 08, 2007 12:15 pm

### WA

I'd made all the test cases correct
but i still got WA why?
The following are my code

Code: Select all

``````#include<iostream>
#include<string>
using namespace std;
long long power(long a,long b);
int main()
{
long long m,n;
long long k=0;
char str[200];
long long rev[1000];
long long sum=0;
bool f=0;
while(cin >> m >> n >> str)
{
for(int i=strlen(str)-1;i>=0;i--)
{
if(isdigit(str[i]))
{
if(str[i]-'0'>m)
{
f = 1;
cout << str<<" is an illegal base "<<m<<" number"<<endl;
break;
}
sum += ((str[i]-'0')*power(m,strlen(str)-i-1));

}
if(str[i]>='A'&&str[i]<='F')
{
if(str[i]-'A'+10>m)
{
f = 1;
cout << str<<" is an illegal base "<<m<<" number"<<endl;
break;
}
sum += ((str[i]-'A'+10)*power(m,strlen(str)-i-1));

}
}
if(sum==0)
rev[k++]=0;
while(sum)
{
rev[k]=sum%n;
sum/=n;
k++;
}
if(f==0)
{
cout << str<<" base "<< m << " = ";
for(int j=k-1;j>=0;j--)
{
if(rev[j]==10)
cout <<'A';
else if(rev[j]==11)
cout <<'B';
else if(rev[j]==12)
cout <<'C';
else if(rev[j]==13)
cout <<'D';
else if(rev[j]==14)
cout <<'E';
else if(rev[j]==15)
cout <<'F';
else
cout << rev[j];
}
cout << " base "<<n;
cout << endl;
}
for(int j=k-1;j>=0;j--)
rev[j]=0;
f=0;
sum =0;
k=0;
}
return 0;
}
long long power(long a,long b)
{
long long result=1;
for(int i=1;i<=b;i++)
{
result*=a;
}
return result;
}``````

abhimanyush10
New poster
Posts: 1
Joined: Sun Dec 16, 2007 2:37 pm

### Help Required (Getting Wrong Answer)

Hey, Iam getting wrong answer even though my code pertains to all the suggestions made by different users for this problem such as leading zero & all possible test cases given on this page. Still some test case is possibly missing & Iam not getting it, please help me as soon as soon as possible.

[code]
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
long ibase,obase,inum,rem,count;
long long numten;
int i,j,flag=0,flag1=0;
long conv[6]={10,11,12,13,14,15};
char numst[6]={'A','B','C','D','E','F'};
char str[100],str1[100],str2[100],str3[100],str4[100],ostr[100],temp[100];
while((scanf("%ld %ld %s",&ibase,&obase,&str))!=EOF)
{
for(i=0;str[i]!='\0';i++)
{
if(((str[i]>='0' && str[i]<='9') && (int)(str[i]-'0')>=ibase) || (((str[i]>='A' && str[i]<='F') && conv[(int)(str[i]-'A')]>=ibase)))
{
flag1=1;
break;
}
}
if(flag1==1)
printf("%s is an illegal base %d number\n",str,ibase);
else
{
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='A' && str[i]<='F')
{
flag=1;
break;
}
}
numten=0;
if(flag==0)
{
for(i=0;str[i]!='\0';i++)
str2[i]=str[i];
str2[i]='\0';
inum=(long)atoi(str);
count=0;
while(inum>0)
{
rem=inum%10;
inum=inum/10;
numten+=((long long)pow(ibase,(float)count))*rem;
count++;
}
}
else
{
for(i=0;str[i]!='\0';i++)
str1[i]=str[i];
str1[i]='\0';
for(j=i-1;j>=0;j--)
temp[i-1-j]=str[j];
temp[i]='\0';
for(j=0;j<=i;j++)
str[j]=temp[j];
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='0' && str[i]<='9')
numten=numten+(long long)(str[i]-'0')*(long)pow(ibase,(float)i);
else if(str[i]>='A' && str[i]<='F')
numten=numten+(long long)pow(ibase,(float)i)*(long)conv[(int)(str[i]-'A')];
}
}
count=0;
while(numten!=0)
{
rem=numten%obase;
numten=numten/obase;
if(rem<10)
ostr[count++]=rem+'0';
else if(rem>=10 && rem<=15)
ostr[count++]=numst[rem-10];
}
ostr[count]='\0';
for(i=0;ostr[i]!='\0';i++)
;
for(j=i-1;j>=0;j--)
temp[i-1-j]=ostr[j];
temp[i]='\0';
for(j=0;j<=i;j++)
ostr[j]=temp[j];
int l=strlen(ostr);
if(l==0)
printf("0 base %d = 0 base %d\n",ibase,obase);
else
{
l=7-l;
if(flag==0)
{
for(i=0;str2[i]=='0';i++)
;
for(j=i;str2[j]!='\0';j++)
str4[j-i]=str2[j];
str4[j-i]='\0';
printf("%s base %d = %s base %d\n",str4,ibase,ostr,obase);
}
else
{
for(i=0;str1[i]=='0';i++)
;
for(j=i;str1[j]!='\0';j++)
str3[j-i]=str1[j];
str3[j-i]='\0';
printf("%s base %d = %s base %d\n",str3,ibase,ostr,obase);
}
}
}
flag1=0;
flag=0;
}
return(0);
}
[/code]

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

### Re: 355 - The Bases Are Loaded

Try the cases.

Input:

Code: Select all

``````10 8 8650286024
15 13 59A566
5 10 3441011134
7 10 3226143411
8 9 3006355015``````
Output:

Code: Select all

``````8650286024 base 10 = 100346161710 base 8
59A566 base 15 = B71686 base 13
3441011134 base 5 = 7750794 base 10
3226143411 base 7 = 134971047 base 10
3006355015 base 8 = 1034758324 base 9``````
Hope these help.
Ami ekhono shopno dekhi...
HomePage