10018 - Reverse and Add
Moderator: Board moderators
Constantly getting compile error
I don't get it. I am constantly getting compile error, although
I don't see any problems with source code. My FreePascal
compiles it with no problem. I tried tu upload code through web page,
I also send it with email. This is the reply I get from judge:
I don't see any problems with source code. My FreePascal
compiles it with no problem. I tried tu upload code through web page,
I also send it with email. This is the reply I get from judge:
the code is here:Here are the compiler error messages:
Free Pascal Compiler version 1.0.6 [2002/05/23] for i386
Copyright (c) 1993-2002 by Florian Klaempfl
Target OS: Linux for i386
Compiling 02170383_24.p
--
PS: Check the board at http://acm.uva.es/board/
Code: Select all
program p10018(input, output);
var
s: string;
i1: byte;
l: integer;
n: integer;
p: longint;
c: longint;
x: longint;
temp: char;
temp2: integer;
resitev1: integer;
resitev2: longint;
function JePalindrom(i: Longint): boolean;
begin
str(i, s);
l := length(s);
JePalindrom := True;
for i1 := 1 to l div 2 do if s[i1] <> s[l + 1 - i1] then
begin
JePalindrom := False;
Break;
end;
end;
function Obrni(i: longint): longint;
begin
str(i, s);
l := length(s);
for i1 := 1 to l div 2 do
begin
temp := s[i1];
s[i1] := s[l + 1 - i1];
s[l + 1 - i1] := temp;
end;
Val(s, Obrni, temp2);
end;
procedure Resi(p: longint);
begin
c := 0;
x := p;
repeat
if JePalindrom(x) then
begin
resitev1 := c;
resitev2 := x;
Exit;
end;
x := x + Obrni(x);
inc(c);
until false;
end;
begin
readln(n);
while n <> 0 do
begin
readln(p);
Resi(p);
Writeln(resitev1, ' ', resitev2);
dec(n);
end;
end.
I think the Pascal compiler that the judge uses does not allow double returning of function value:
[pascal]function JePalindrom(i: Longint): boolean;
begin
str(i, s);
l := length(s);
JePalindrom := True;
for i1 := 1 to l div 2 do if s[i1] <> s[l + 1 - i1] then
begin
JePalindrom := False;
Break;
end;
end;[/pascal]
Try to use a temporary Boolean variable instead. Maybe this can help.
[pascal]function JePalindrom(i: Longint): boolean;
begin
str(i, s);
l := length(s);
JePalindrom := True;
for i1 := 1 to l div 2 do if s[i1] <> s[l + 1 - i1] then
begin
JePalindrom := False;
Break;
end;
end;[/pascal]
Try to use a temporary Boolean variable instead. Maybe this can help.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
10018 - How to reverse an int?
Hey guys
what's the basic algorithm to reverse and int in Java?
Thanks
what's the basic algorithm to reverse and int in Java?
Thanks
Re: 10018 - How to reverse an int?
Something like:OutCaster wrote:Hey guys
what's the basic algorithm to reverse and int in Java?
Thanks
Code: Select all
while (N) {
R = R*10+N%10;
N /= 10;
}
Saludos,
HoraPe
10018
I am a beginner, so I know that probably my code is so inefficient...
But I don't know exactly why it gives TLE. or Runtime error.
If I make the array bigger the judge gives me TLE., if not the judge says Run time error...
[c]
#include <string.h>
int main()
{
char n; /*cases*/
int i=1,j,k,z; /*counters*/
char pal[15],pal2[15]; /*N
But I don't know exactly why it gives TLE. or Runtime error.
If I make the array bigger the judge gives me TLE., if not the judge says Run time error...
[c]
#include <string.h>
int main()
{
char n; /*cases*/
int i=1,j,k,z; /*counters*/
char pal[15],pal2[15]; /*N
10018 - Reverse and Add
I've read all the topics about this problem and my program gives correct answers, i think. But when I submit the program, it gives a Time Limit Exceeded Error. Can anyone explain me why?
Thanks...
Code: Select all
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int IsPalindrome();
void reverse();
char number[11];
unsigned long int num, temp;
int N, i, a;
int main(int argc, char* argv[])
{
scanf("%d", &N);
for(i=0; i<N; i++)
{
scanf("%lu", &temp);
for(a=0; !IsPalindrome(); a++)
{
reverse();
temp+=num;
}
printf("%d %lu\n", a, temp);
}
return 0;
}
int IsPalindrome()
{
int c, b;
memset(number, '\0', 13);
sprintf(number, "%lu", temp);
for(c=0, b=strlen(number)-1; b>=c; c++, b--)
{
if(number[c]!=number[b])
return 0;
}
if(a==0)
return 0;
return 1;
}
void reverse()
{
int d, e;
char aux;
for(d=0, e=strlen(number)-1; d<e; d++, e--)
{
aux=number[d];
number[d]=number[e];
number[e]=aux;
}
num=atoi(number);
}
-
- New poster
- Posts: 17
- Joined: Thu Jul 15, 2004 10:55 am
- Location: Poland, Rzeszow University of Technology
10018 WA in C
Hi
I read all topics about this problem.
I use unsigned long int, and use "%lu" in printf and scanf.
I try inputs below.
When I input 4000000000, my computer just running.
It didn't have any ouput.
I have tried use long long, but I don't know what it means.
I couldn't find where is wrong.
Could anyone give me some tips?
Thanks!!!
[c]
#include <stdio.h>
#define max 3000
unsigned long int reverse(unsigned long int P)
{
unsigned long int aux, fator, resp;
aux = P;
resp = 0;
fator = 1;
while ( aux != 0 )
{
fator = fator * 10;
aux = aux / 10;
}
fator = fator / 10;
aux = P;
while ( P != 0 )
{
aux = P % 10;
resp = resp + aux * fator;
P = P / 10;
fator = fator / 10;
}
return resp;
}
unsigned long int total(unsigned long int P)
{
P = reverse(P) + P;
return P;
}
main()
{
unsigned long int N, i, P[max], min[max];
scanf("%lu", &N);
for ( i = 0 ; i < N ; i++ )
{
scanf("%lu", &P);
while (1)
{
P = total(P);
min++;
if ( P == reverse(P) )
break;
}
}
for ( i = 0 ; i < N ; i++ )
printf("%lu %lu\n", min, P);
}
[/c]
I read all topics about this problem.
I use unsigned long int, and use "%lu" in printf and scanf.
I try inputs below.
Code: Select all
Input
2
2
99
Output
1 4
6 79497
It didn't have any ouput.
I have tried use long long, but I don't know what it means.
I couldn't find where is wrong.
Could anyone give me some tips?
Thanks!!!
[c]
#include <stdio.h>
#define max 3000
unsigned long int reverse(unsigned long int P)
{
unsigned long int aux, fator, resp;
aux = P;
resp = 0;
fator = 1;
while ( aux != 0 )
{
fator = fator * 10;
aux = aux / 10;
}
fator = fator / 10;
aux = P;
while ( P != 0 )
{
aux = P % 10;
resp = resp + aux * fator;
P = P / 10;
fator = fator / 10;
}
return resp;
}
unsigned long int total(unsigned long int P)
{
P = reverse(P) + P;
return P;
}
main()
{
unsigned long int N, i, P[max], min[max];
scanf("%lu", &N);
for ( i = 0 ; i < N ; i++ )
{
scanf("%lu", &P);
while (1)
{
P = total(P);
min++;
if ( P == reverse(P) )
break;
}
}
for ( i = 0 ; i < N ; i++ )
printf("%lu %lu\n", min, P);
}
[/c]
-
- New poster
- Posts: 13
- Joined: Tue Aug 03, 2004 4:24 am
- Location: CHINA
Thanks
I don't think so.
I got AC,and I use "%lu"
I got AC,and I use "%lu"
Code: Select all
# include <stdio.h>
int main()
{
unsigned long num,rev,temp,feng;
unsigned int i,j,k,count,time;
scanf("%d",&time);
for(i=0;i<time;i++)
{
scanf("%lu",&num);
count=0;
while(1)
{
rev=0;
for(j=1000000000;j>0;j=j/10)
{
if(num/j>0)
break;
}
temp=num;
k=j;
while(temp!=0)
{
rev=rev+(temp%10)*j;
temp=temp/10;
j=j/10;
}
if(rev==num)
{
printf("%d %lu\n",count,num);
break;
}
else
{
feng=num;
count++;
while(num!=0)
{
feng=feng+(num%10)*k;
num=num/10;
k=k/10;
}
num=feng;
}
}
}
return 0;
}