Page 6 of 7
Re: 389 - 8 months? WA
Posted: Mon Jan 17, 2011 4:12 pm
by kissu parina
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....
Re: 389 WA why?
Posted: Sun Sep 11, 2011 10:20 pm
by shamsacm
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
Re: 389- Basically Speaking, WA
Posted: Fri Dec 30, 2011 9:06 am
by sadia_atique
Why am I getting a presentation error??can anyone please help me??
Re: 389- Basically Speaking, WA
Posted: Fri Dec 30, 2011 9:11 am
by sohel
Not sure whether this is the only reason, but you are printing ERROR from the first column - ERROR should also be justified.
Re: 389- Basically Speaking, WA
Posted: Fri Dec 30, 2011 9:23 am
by sadia_atique
Thanks a lot!!!got ACed

389
Posted: Sun Jul 15, 2012 7:40 pm
by cse.mehedi
Re: 389
Posted: Tue Jul 17, 2012 3:29 am
by brianfry713
On the sample I/O everything is not right justified.
Re: 389
Posted: Tue Jul 17, 2012 2:05 pm
by cse.mehedi
brianfry713 wrote:On the sample I/O everything is not right justified.
Thank u Brianfry.
Re: 389
Posted: Fri Jun 28, 2013 11:28 am
by ashdboss
Code deleted after getting AC.
Re: 389
Posted: Fri Jun 28, 2013 1:42 pm
by t.tahasin
@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.

Re: 389
Posted: Fri Jun 28, 2013 1:51 pm
by ashdboss
AC.
Thank u very much... t.tahasin
Re: 389
Posted: Thu Oct 17, 2013 2:58 pm
by hungphongbk
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)
{
car = System.in.read();
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--)
{
rs=rs.add(cs.multiply(BigInteger.valueOf(toVal(x.charAt(i)))));
cs=cs.multiply(bs);
}
return rs;
}
static boolean calctest()
{
String s=ReadLn(2000);
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);
}
}
Re: 389
Posted: Thu Oct 17, 2013 8:00 pm
by brianfry713
Try using BufferedReader and BufferedWriter.
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
Re: 389
Posted: Tue Apr 01, 2014 7:27 pm
by Shihab
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;
}
Re: 389
Posted: Tue Apr 01, 2014 9:30 pm
by brianfry713
try running your code on the sample input