## 10018 - Reverse and Add

Moderator: Board moderators

WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am
Hi Sohel,
thanks for the hint.

The program has been accepted now. I don't know why, though.

The problem states: '' ... choose a number, reverse its digits and
add.... If the sum is not a palindrome...''

So I understood you have to reverse and add at least once.

But ok, thanks a lot.

Betalord
New poster
Posts: 9
Joined: Sat May 03, 2003 11:36 pm
Location: Slovenia

### 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:
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/
the code is here:

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
while n <> 0 do
begin
Resi(p);
Writeln(resitev1, ' ', resitev2);
dec(n);
end;

end.
``````

Betalord
New poster
Posts: 9
Joined: Sat May 03, 2003 11:36 pm
Location: Slovenia
No one knows?? Any ideas?

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong
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.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

OutCaster
New poster
Posts: 4
Joined: Thu Jan 22, 2004 2:24 am

### 10018 - How to reverse an int?

Hey guys

what's the basic algorithm to reverse and int in Java?

Thanks

horape
New poster
Posts: 49
Joined: Sat Sep 13, 2003 3:18 pm
Location: Buenos Aires

### Re: 10018 - How to reverse an int?

OutCaster wrote:Hey guys

what's the basic algorithm to reverse and int in Java?

Thanks
Something like:

Code: Select all

``````while (N) {
R = R*10+N%10;
N /= 10;
}
``````
should work.

Saludos,
HoraPe

OutCaster
New poster
Posts: 4
Joined: Thu Jan 22, 2004 2:24 am
cool thanks

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

### 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,pal2; /*N

prince56k
New poster
Posts: 33
Joined: Fri Dec 12, 2003 10:32 pm
Hi,

Do u get answer for the sample I/O ?
I faced TLE in the Sample I/O in your code Moreover , u first take n as total number of input. But u never create a loop to take n inputs.

New poster
Posts: 10
Joined: Mon Jul 19, 2004 6:35 pm

### 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?

Code: Select all

``````#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int IsPalindrome();
void reverse();

char number;
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);

}
``````
Thanks...

Piotrek Mazur
New poster
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology
Even if you won't have TLE, you will have WA after submitting this. Think about situation when on input is already a palindrome - there is no need to make "Reverse and Add" on it.

New poster
Posts: 10
Joined: Mon Jul 19, 2004 6:35 pm
i removed the "if(a==o) return 0 " in the IsPalindrome() function and then the program just print 0 and the number if it (the number) is a palindrome. then i submit it and now it gives a runtime error. "File size limit exceeded". I can't understand why. Can someone help me? Thanks

Zoe
New poster
Posts: 9
Joined: Tue Jul 20, 2004 5:46 am
Location: Taiwen

### 10018 WA in C

Hi
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
``````
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]

Zoe
New poster
Posts: 9
Joined: Tue Jul 20, 2004 5:46 am
Location: Taiwen
I got AC!
I use long long and "%lld" in printf and scanf.

JuaingFall
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"

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;
}``````