10473 - Simple Base Conversion
Moderator: Board moderators
10473 - Simple Base Conversion
When will these problems occure ??
-
- System administrator & Problemsetter
- 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.
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.
-
- 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
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.
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
NO sigNature
-
- Experienced poster
- Posts: 131
- Joined: Thu Apr 17, 2003 8:39 am
- Location: Baku, Azerbaijan
-
- System administrator & Problemsetter
- Posts: 399
- Joined: Sat Jan 12, 2002 2:00 am
thanks!
thanks
!
![8)](./images/smilies/icon_cool.gif)
10473 WA??? Please help me!
![:o](./images/smilies/icon_eek.gif)
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]
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.
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.
![:)](./images/smilies/icon_smile.gif)
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.
-
- Learning poster
- Posts: 90
- Joined: Sat Feb 15, 2003 1:39 am
- Location: Paris, France
- 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[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]
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]