Page 3 of 5
Posted: Sat Mar 17, 2007 3:52 pm
by Jan
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.
Posted: Sun Mar 18, 2007 10:56 am
by jainal cse du
removed
dear Jainal
Posted: Sun Mar 18, 2007 12:03 pm
by ishtiaq ahmed
I have tried this input as follows:
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
you should omit the leading zeros and as well as your repetance your output.
Best of luck
Posted: Sun Mar 18, 2007 12:08 pm
by Jan
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:
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:
you should omit the leading zeros and as well as your repetance your output.
I think it is not a valid case. Because my code returns
So, you don't have to think about leading zeroes.
Posted: Sun Mar 18, 2007 2:22 pm
by jainal cse du
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");
}
Posted: Mon Mar 19, 2007 6:35 pm
by Jan
Replace the following unnecessary line
Code: Select all
else if('1'>= str[i] && str[i] <= '9')
with just
Hope this works.
getting WA in 355
Posted: Fri Apr 13, 2007 3:06 pm
by mrunmoy
Dear Gurus,
Need your help in finding out the reason for WA.
i didnt take care about the power function for unsigned long long case.
It got AC after change.
Thanks a bunch!
Posted: Fri Apr 13, 2007 3:35 pm
by mf
Try
Correct outputs:
Code: Select all
1000000000 base 16 = 68719476736 base 10
FEDCBA9876 base 16 = 1094624909430 base 10
Thanks mf
Posted: Fri Apr 13, 2007 4:01 pm
by mrunmoy
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...
leading zeros doesn't matter
Posted: Sat Jun 30, 2007 3:58 am
by zid_adrenalyns
ishtiaq ahmed wrote:I have tried this input as follows:
you should omit the leading zeros and as well as your repetance your output.
I think it is not a valid case. Because my code returns
So, you don't have to think about leading zeroes.
I'm agree with you, both outputs are correct!
Code: Select all
0001 base 2 = 1 base 2
0001 base 2 = 0001 base 2
Posted: Sat Jul 21, 2007 9:32 pm
by spider_6765
can 'unsigned long long' can hold the value of 9999999999(decimal)?
Posted: Sun Jul 22, 2007 12:07 pm
by Jan
'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.
WA
Posted: Sun Jul 29, 2007 8:45 am
by handsomepot
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;
}
Help Required (Getting Wrong Answer)
Posted: Thu Apr 10, 2008 7:12 pm
by abhimanyush10
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]
Re: 355 - The Bases Are Loaded
Posted: Fri Apr 11, 2008 12:45 am
by Jan
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.