Page 1 of 4

10473 - Simple Base Conversion

Posted: Thu Apr 10, 2003 8:00 pm
by Pasq
When will these problems occure ??

Busy

Posted: Fri Apr 11, 2003 12:20 am
by shahriar_manzoor
Hello,
Actually the doc files of these problems are such that converting them to readable html will require a lot of hand editing. I am very busy now with some more intelligent work (HTML editing is not an intelligent work) so there is this delay. I hope within a week or 10 days they will appear.

10473 TLE

Posted: Sun Apr 13, 2003 1:50 pm
by Eric
It seems to be easy. But why do I get TLE?
Can anyone give me a hand?

Posted: Sun Apr 13, 2003 2:05 pm
by cytse
Input terminates with a negative decimal number, not necessarily -1

Posted: Sun Apr 13, 2003 3:43 pm
by Eric
I see. I overlook the problem description.
Thanks very much.

10473 give me some test

Posted: Sat Apr 19, 2003 11:59 pm
by Farid Ahmadov
Hello.

Here is my code:
[pascal]program Simple_Base_Convertion;

const
hex : array[0..15] of char = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');

var
S: string;
i: integer;
d: longint;

function decm(X: char): integer;
begin
if X in ['0'..'9'] then decm:=ord(X)-48 else
if X in ['A'..'F'] then decm:=ord(X)-55;
end;

begin
readln(S);
while (S[1]<>'-') do
begin
if S[1]+S[2]='0x' then
begin
delete(S,1,2);
d:=0;
for i:=1 to length(S) do d:=d*16+decm(S);
writeln(d);
end else
begin
for i:=1 to length(S) do d:=d*10+ord(S)-48;
S:='';
while d<>0 do
begin
S:=hex[d mod 16]+S;
d:=d div 16;
end;
writeln('0x'+S);
end;
readln(S);
end;
end.
[/pascal]

I always get WA. What is the problem? What's wrong with this program?
If you can please fix my errors or give me some test to check myself.

It works on all my tests.

Posted: Sun Apr 20, 2003 12:26 pm
by Eric
input
  • 291
    0x123
    291
Your program's output:
  • 0x123
    291
    0x471DB
Correct output:
  • 0x123
    291
    0x123
Do you see your mistake?

Posted: Sun Apr 20, 2003 12:56 pm
by Farid Ahmadov
Thank you very much Eric.
But I have already got AC with another program.

Posted: Sun Apr 20, 2003 6:37 pm
by soyoja
I always appreciate your effort for this site... ^^

Maybe someone want to solve new problem, so they demand you..

I believe that you are a man of understanding sense...

Thanks.

thanks!

Posted: Sun Apr 20, 2003 11:49 pm
by shahriar_manzoor
thanks 8) !

10473 WA??? Please help me!

Posted: Sat May 10, 2003 4:44 am
by AaronWu
:o
What's Wrong with my code?
It runs correctly in my computer,but how can I get a WA for it?
Seek help!!!
here's my code :


[cpp]#include <iostream.h>
#include <stdio.h>
long pow(int, int);
void cal(void);
void change(int);
int main()
{ char ch;
ch = getchar();
while(1)
{
if (ch == '-')
return 1;
else if ((ch == '\n') || (ch == ' '))
{ ch = getchar();
continue;
}
else if ( ch == '0')
{ ch = getchar();
if ((ch =='x')||(ch == 'X'))
cal();
else { printf("0x0\n");
ch = getchar();
continue;
}
}
else change(ch-'0');
ch = getchar();
}

}


long pow( int x,int y)
{ int i;
long result = 1;
for(i = 0; i < y; i++)
result*=x;
return result;
}

void cal(void)
{ char at;
long t = 0;
long answer = 0;
int i = 0;
at = getchar();
while(1)
{ if ((at == ' ')||(at == '\n'))
break;

else t = t * 10 + at - '0';
at = getchar();
}
while(t)
{ answer+=(t%10)*pow(16,i++);
t=t/10;
}
printf("%ld\n",answer);
}


void change(int n)
{ char at;
long t = n;
at = getchar();
while(1)
{ if ((at == ' ')||(at == '\n'))
break;
else t = t * 10 + at - '0';
at = getchar();
}

printf("0x%X\n", t);
}[/cpp][/b][/cpp]

Posted: Tue May 13, 2003 9:51 pm
by Sebasti
Hi,

First, your program is too complex. Look the usage of functions like strtol, atoi and the iosflags of C++, and you will see that the size of your code will decrease. :)

Second, when the input begin with 0 and the other caracter is not x, you must no print 0, because the number could be a decimal number like 010.

Bye.

Posted: Wed May 14, 2003 1:04 pm
by bery olivier
[quote="Sebasti

Posted: Fri May 16, 2003 3:30 am
by AaronWu
Oh,yes.First I should thank you for giving me your advice! But I have found something else wrong with my code
printf("0x%lX\n", t);
beside that I also change function cal() because I did not notice the useage of "A\B\C\D\E\F" and now I got AC for it.
8)[/cpp][/list]

10473

Posted: Fri May 28, 2004 5:00 am
by midra
I got WA but it seems to be ok...
I test several Input/Output and it gives correct answer

Here is my code:

[c]
#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
char n[50];
int temp;
int i,j;
int res[50];
while(1)
{
temp=j=0;
scanf("%s",&n);
if (n[0]=='-')
break;
if (n[0]=='0' && n[1]=='x')
{
for (j=0,i=strlen(n)-1;i>=2;i--,j++)
{
if (n<=64)
temp+=(n-48)*pow(16,j);
else if (n=='A')
temp+=10*pow(16,j);
else if (n=='B')
temp+=10*pow(16,j);
else if (n=='C')
temp+=10*pow(16,j);
else if (n=='D')
temp+=10*pow(16,j);
else if (n=='E')
temp+=10*pow(16,j);
else if (n=='F')
temp+=10*pow(16,j);
}

printf("%d",temp);
}
else
{
for (j=0,i=strlen(n)-1;i>=0;i--,j++)
temp+=(n-48)*pow(10,j);
j=0;
while(temp>0)
{
res[j]=temp%16;
temp=temp/16;
j++;
}
printf("0x");
for (i=j-1;i>=0;i--)
{
if (res>9)
{
if (res[i]==10)
printf("A");
else if (res[i]==11)
printf("B");
else if (res[i]==12)
printf("C");
else if (res[i]==13)
printf("D");
else if (res[i]==14)
printf("E");
else if (res[i]==15)
printf("F");
}
else
printf("%d",res[i]);
}
}
printf("\n");
}
return 0;
[/c]