## 389 - Basically Speaking

Moderator: Board moderators

kissu parina
New poster
Posts: 19
Joined: Thu May 20, 2010 8:58 am

### Re: 389 - 8 months? WA

can some1 tell what is wrong with this-->

Code: Select all

``````#include<iostream>
using namespace std;
#include<list>
#include<cmath>
#include<map>
#include<cstring>
long toDecimal(char *s,int cur_base){
map<char,int>M;M['1']=1;M['2']=2;M['3']=3;M['4']=4;M['5']=5;M['6']=6;M['7']=7;M['8']=8;M['9']=9;M['0']=0;
M['A']=10;M['B']=11;M['C']=12;M['D']=13;M['E']=14;M['F']=15;
long ans=0;
int len=strlen(s)-1,power=0;
while(len>=0){
ans+=M[*(s+len)]*(int)pow((double)cur_base,(double)power++);
len--;
}
return ans;
}
void toBase( long dec,int b){
char str[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
list<char>L;
char jon[10001];
long ba=b,da=dec,inx=0;
while(dec){
int ses=dec%b;
long fol=dec/b;
L.push_front(str[ses]);
dec=fol;
}
list<char>::iterator p=L.begin();
while(p!=L.end()){
jon[inx++]=*p;
p++;
}
jon[inx]='\0';
char dd[]={"ERROR"};
if(inx>7)printf("%7s\n",dd);
else printf("%7s\n",jon);

}
int main(){
char s[10001];
int from,to;
while(cin>>s>>from>>to){
long dec;
if(s[0]=='0')printf("      0\n");
else{
dec=toDecimal(s,from);
if(to==10)printf("%7ld\n",dec);
else toBase(dec,to);
}
}
return 0;
}
``````
regards....
one day...

shamsacm
New poster
Posts: 6
Joined: Sat May 07, 2011 12:45 pm

### Re: 389 WA why?

try this
input

Code: Select all

``````9999999 16 16
ssssss0 10 12
ssss000 10 12
0000000 10 12
sssssFF 16  2``````
Output

Code: Select all

``````9999999
ssssss0
ssssss0
ssssss0
ssERROR``````
Here s means space
Ha ha ha I got AC After 15 WA for input 0000000 10 12

New poster
Posts: 25
Joined: Thu Nov 24, 2011 6:32 am

### Re: 389- Basically Speaking, WA

Code: Select all

``````      /*Removed after ACed*/
``````
Last edited by sadia_atique on Fri Dec 30, 2011 9:28 am, edited 1 time in total.

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

### Re: 389- Basically Speaking, WA

Not sure whether this is the only reason, but you are printing ERROR from the first column - ERROR should also be justified.

New poster
Posts: 25
Joined: Thu Nov 24, 2011 6:32 am

### Re: 389- Basically Speaking, WA

Thanks a lot!!!got ACed

cse.mehedi
New poster
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

### 389

Code: Select all

``````AC
``````
Last edited by cse.mehedi on Tue Jul 17, 2012 2:03 pm, edited 3 times in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 389

On the sample I/O everything is not right justified.
Check input and AC output for thousands of problems on uDebug!

cse.mehedi
New poster
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

### Re: 389

brianfry713 wrote:On the sample I/O everything is not right justified.
Thank u Brianfry.

ashdboss
New poster
Posts: 16
Joined: Fri May 17, 2013 8:59 am

### Re: 389

Code deleted after getting AC.
Last edited by ashdboss on Fri Jun 28, 2013 1:56 pm, edited 1 time in total.

t.tahasin
New poster
Posts: 38
Joined: Tue May 28, 2013 11:21 pm

### Re: 389

@ashdboss:
I think the problem is with your string_rev() function. try to avoid returning pointer. Rather returning the pointer from the function I always pass another array to the function as parameter.
anyways, you can change your string_rev() as follows

Code: Select all

``````char* string_rev(char x[] )
{
char y[100000];
for(int i= (int)strlen(x)-1,j=0; i>=0; i--,j++)//0 index
{
y[j] = x[i];
}
y[strlen(x)]='\0';
return y;
}
``````
to

Code: Select all

``````char* string_rev(char x[] )
{
char *y = (char*)malloc (strlen(x) * sizeof (char));
for(int i= (int)strlen(x)-1,j=0; i>=0; i--,j++)//0 index
{
y[j] = x[i];
}
y[strlen(x)]='\0';
return y;
}
``````
hope this will help.

ashdboss
New poster
Posts: 16
Joined: Fri May 17, 2013 8:59 am

### Re: 389

AC. Thank u very much... t.tahasin

hungphongbk
New poster
Posts: 2
Joined: Sat Oct 12, 2013 6:01 pm

### Re: 389

Help me, my code is getting TLE... I couldn't found where error occured

Code: Select all

``````import java.io.*;
import java.lang.String;
import java.math.BigInteger;
import java.util.*;

class Main
{
static StringTokenizer st;
static String N;

static String ReadLn (int maxLg)  // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try
{
while (lg < maxLg)
{
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null);  // eof
return (new String (lin, 0, lg));
}
static int toVal(char c)
{
if (c<='9') return c-'0';
else return c-'A'+10;
}
static BigInteger toBase10(String x,int base)
{
BigInteger bs=BigInteger.valueOf(base);
BigInteger cs=BigInteger.ONE;
BigInteger rs=BigInteger.ZERO;
for (int i=x.length()-1;i>=0;i--)
{
cs=cs.multiply(bs);
}
return rs;
}
static boolean calctest()
{
if (s==null) return false;
s=s.trim();
if (s.length()==0) return false;
st=new StringTokenizer(s);
N=st.nextToken();
int baseIn=Integer.parseInt(st.nextToken());
int baseOut=Integer.parseInt(st.nextToken());

BigInteger RS=toBase10(N,baseIn);
String rs=RS.toString(baseOut).toUpperCase();
if (rs.length()>7) rs="ERROR";
System.out.println(String.format("%7s",rs));
return true;
}
public static void main(String args[])
{
do
{
if (!calctest())
break;
}
while (true);
}
}``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 389

Try using this to convert the input to a BigInteger instead of first converting to base 10:
http://docs.oracle.com/javase/6/docs/ap ... ,%20int%29
Check input and AC output for thousands of problems on uDebug!

Shihab
New poster
Posts: 33
Joined: Thu Jun 13, 2013 1:19 pm

### Re: 389

WA , help

Code: Select all

``````
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

long long  power(int base,long long p)
{
long long i=0,result=1;

for(; i<p; i++)
{
result*=base;
}

return result;

}

int char_to_int(char c)
{
return c-55;
}

int f1(int n)
{
return 55+n;
}

void f(long long number,int base2)
{
char string1[100000],string2[100000],error[10]={'E','R','R','O','R','\0'};
int i=0,j,k;

while(number!=0)
{
string1[i]=(number%base2)+'0';
//printf("%c\n",string1[i]);

if((number % base2) >9)
{
string1[i]=f1(number%base2);
}

i++;

number/=base2;
}
string1[i]='\0';

if(i>7)
printf("%7s\n",error);
else
{

i--;

j=0;
while(i>=0)
{
string2[j++]=string1[i--];
}

string2[j]='\0';

printf("%7s\n",string2);

}

}

long long  base_convert(char s[],long long len,int base)
{

int i=0;
long long temp;
long long base2_number=0;

while(s[i]!='\0')
{
temp=s[i]-'0';

if(s[i]>='A' && s[i]<='Z')
temp=char_to_int(s[i]);

base2_number+=(temp * power(base,len));
len--;

i++;
}

return base2_number;
}

int main()
{

long long base1,base2,flag,base2_number,len,v,i;

char base1_number[100000];

while(scanf("%s %lld %lld",base1_number,&base1,&base2)==3)
{

i=0;
flag=0;
v=0;

if(base1_number[0]=='0')
printf("%7lld\n",v);
else
{
//            while(base1_number[i]!='\0')
//            {
//                v=base1_number[i]-'0';
//
//                if(base1_number[i]>='A' && base1_number[i]<='Z')
//                    v=char_to_int(base1_number[i]);
//
//                i++;
//
//            }

//if(!flag)
{
len=strlen(base1_number)-1;

base2_number=base_convert(base1_number,len,base1);

if(base2==10)
{
printf("-%7lld\n",base2_number);
}
else
{

f(base2_number,base2);

}

}

}

}

return 0;
}

``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 389

try running your code on the sample input
Check input and AC output for thousands of problems on uDebug!