10473 - Simple Base Conversion

All about problems in Volume 104. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Pasq
New poster
Posts: 5
Joined: Mon Jun 24, 2002 5:53 pm

10473 - Simple Base Conversion

Post by Pasq »

When will these problems occure ??
shahriar_manzoor
System administrator & Problemsetter
Posts: 399
Joined: Sat Jan 12, 2002 2:00 am

Busy

Post 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.
Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

10473 TLE

Post by Eric »

It seems to be easy. But why do I get TLE?
Can anyone give me a hand?
Last edited by Eric on Sun Apr 13, 2003 3:42 pm, edited 1 time in total.
User avatar
cytse
Learning poster
Posts: 67
Joined: Mon Sep 16, 2002 2:47 pm
Location: Hong Kong
Contact:

Post by cytse »

Input terminates with a negative decimal number, not necessarily -1
Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Post by Eric »

I see. I overlook the problem description.
Thanks very much.
Farid Ahmadov
Experienced poster
Posts: 131
Joined: Thu Apr 17, 2003 8:39 am
Location: Baku, Azerbaijan

10473 give me some test

Post 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.
_____________
NO sigNature
Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Post by Eric »

input
  • 291
    0x123
    291
Your program's output:
  • 0x123
    291
    0x471DB
Correct output:
  • 0x123
    291
    0x123
Do you see your mistake?
Farid Ahmadov
Experienced poster
Posts: 131
Joined: Thu Apr 17, 2003 8:39 am
Location: Baku, Azerbaijan

Post by Farid Ahmadov »

Thank you very much Eric.
But I have already got AC with another program.
_____________
NO sigNature
soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:

Post 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.
shahriar_manzoor
System administrator & Problemsetter
Posts: 399
Joined: Sat Jan 12, 2002 2:00 am

thanks!

Post by shahriar_manzoor »

thanks 8) !
AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

10473 WA??? Please help me!

Post 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]
Sebasti
New poster
Posts: 10
Joined: Sun Apr 13, 2003 11:41 pm
Contact:

Post 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.
bery olivier
Learning poster
Posts: 90
Joined: Sat Feb 15, 2003 1:39 am
Location: Paris, France
Contact:

Post by bery olivier »

[quote="Sebasti
Not AC yet Image AC at last Image
AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

Post 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]
midra
Experienced poster
Posts: 119
Joined: Fri Feb 13, 2004 7:20 am
Contact:

10473

Post 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]
Post Reply

Return to “Volume 104 (10400-10499)”