Input:
Code: Select all
10 16 58
10 5 44
10 2 62342
10 13 1
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
Moderator: Board moderators
Code: Select all
10 16 58
10 5 44
10 2 62342
10 13 1
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
Code: Select all
2 2 0001
Code: Select all
0001 base 2 = 0001 base 2
0001 base 2 = 110010000000101000011101010001 base 2
Code: Select all
1 base 2 = 1 base 2
Try the cases...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.
Code: Select all
14 14 5A144D49
16 16 4
Code: Select all
5A144D49 base 14 = 5A144D49 base 14
4 base 16 = 4 base 16
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--;
}
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;
}
I think it is not a valid case. Because my code returnsishtiaq ahmed wrote:I have tried this input as follows:you should omit the leading zeros and as well as your repetance your output.Code: Select all
2 2 0001
Code: Select all
0001 base 2 = 1 base 2
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);
}
Code: Select all
else if('1'>= str[i] && str[i] <= '9')
Code: Select all
else
Code: Select all
ERASED
Code: Select all
16 10 1000000000
16 10 FEDCBA9876
Code: Select all
1000000000 base 16 = 68719476736 base 10
FEDCBA9876 base 16 = 1094624909430 base 10
I think it is not a valid case. Because my code returnsishtiaq ahmed wrote:I have tried this input as follows:you should omit the leading zeros and as well as your repetance your output.Code: Select all
2 2 0001
Code: Select all
0001 base 2 = 1 base 2
Code: Select all
0001 base 2 = 1 base 2
0001 base 2 = 0001 base 2
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;
}
Code: Select all
10 8 8650286024
15 13 59A566
5 10 3441011134
7 10 3226143411
8 9 3006355015
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