## 10473 - Simple Base Conversion

Moderator: Board moderators

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

### 10473 - Simple Base Conversion

When will these problems occure ??

shahriar_manzoor
Posts: 399
Joined: Sat Jan 12, 2002 2:00 am

### Busy

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

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.

cytse
Learning poster
Posts: 67
Joined: Mon Sep 16, 2002 2:47 pm
Location: Hong Kong
Contact:
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
I see. I overlook the problem description.
Thanks very much.

Experienced poster
Posts: 131
Joined: Thu Apr 17, 2003 8:39 am
Location: Baku, Azerbaijan

### 10473 give me some test

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
while (S<>'-') do
begin
if S+S='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;
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
input
• 291
0x123
291
• 0x123
291
0x471DB
Correct output:
• 0x123
291
0x123

Experienced poster
Posts: 131
Joined: Thu Apr 17, 2003 8:39 am
Location: Baku, Azerbaijan
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:
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
Posts: 399
Joined: Sat Jan 12, 2002 2:00 am

### thanks!

thanks !

AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am 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;
int i = 0;
at = getchar();
while(1)
{ if ((at == ' ')||(at == '\n'))
break;

else t = t * 10 + at - '0';
at = getchar();
}
while(t)
t=t/10;
}
}

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:
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:
[quote="Sebasti
Not AC yet AC at last AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am
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. [/cpp][/list]

midra
Experienced poster
Posts: 119
Joined: Fri Feb 13, 2004 7:20 am
Contact:

### 10473

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;
int temp;
int i,j;
int res;
while(1)
{
temp=j=0;
scanf("%s",&n);
if (n=='-')
break;
if (n=='0' && n=='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]