Page 4 of 12

Posted: Wed Dec 10, 2003 1:38 pm
by WR
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.

Constantly getting compile error

Posted: Thu Dec 25, 2003 5:31 pm
by Betalord
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
  readln(n);
  while n <> 0 do
  begin
    readln(p);
    Resi(p);
    Writeln(resitev1, ' ', resitev2);
    dec(n);
  end;

end.

Posted: Thu Jan 01, 2004 1:42 am
by Betalord
No one knows?? Any ideas?

Posted: Thu Jan 01, 2004 4:47 am
by Observer
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.

10018 - How to reverse an int?

Posted: Sat Feb 07, 2004 8:53 pm
by OutCaster
Hey guys

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

Thanks

Re: 10018 - How to reverse an int?

Posted: Sun Feb 08, 2004 2:11 am
by horape
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

Posted: Sun Feb 08, 2004 6:50 am
by OutCaster
cool thanks

10018

Posted: Tue Apr 20, 2004 4:35 am
by midra
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

Posted: Fri Apr 23, 2004 7:55 am
by prince56k
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.

10018 - Reverse and Add

Posted: Wed Jul 21, 2004 1:54 pm
by vadiu
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[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);


}
Thanks...

Posted: Wed Jul 21, 2004 3:47 pm
by Piotrek Mazur
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.

Posted: Wed Jul 21, 2004 4:25 pm
by vadiu
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

10018 WA in C

Posted: Mon Jul 26, 2004 8:37 am
by Zoe
Hi
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
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]

Posted: Mon Jul 26, 2004 9:14 am
by Zoe
I got AC!
I use long long and "%lld" in printf and scanf.

Thanks

Posted: Tue Aug 03, 2004 9:56 am
by JuaingFall
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;
}