10018 - Reverse and Add

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 pm

Post by Almost Human » Fri May 23, 2003 9:46 am

I've tried but it is still WA until I change all unsigned long into float ...

now I got ACC ...

Thanks for the suggestion .

Hisoka
Experienced poster
Posts: 120
Joined: Wed Mar 05, 2003 10:40 am
Location: Indonesia

Post by Hisoka » Fri May 23, 2003 7:02 pm

are you sure, I just change %li to %lu at input variabel, I submit your program and give me AC. :-?

titid_gede
Experienced poster
Posts: 187
Joined: Wed Dec 11, 2002 2:03 pm
Location: Mount Papandayan, Garut

Post by titid_gede » Fri May 23, 2003 9:00 pm

perhaps you only changed %li to %lu only for scanf. you didnt change another %li (printf).
Kalo mau kaya, buat apa sekolah?

passwd
New poster
Posts: 14
Joined: Thu Dec 05, 2002 11:20 pm

Post by passwd » Tue May 27, 2003 2:50 am

hi, I'm trying to solve this problem too , but I'm getting WA and I don't
know why ??

someone can help me ??

[c]
#include <stdio.h>
/*#define DEBUG*/

long reverse(long n) {
long fator=1;
long aux=n,resp=1;
while(aux!=0) { fator*=10; aux = aux / 10; }
fator/=10;
/* printf("aux: %d fator:%d \n",aux,fator); */
do {
aux=n % 10;
resp = resp + aux * fator;
n= n / 10;
fator/=10;
} while(n!=0);
--resp;
return resp;
}

int palindrome(long n) {
long aux=reverse(n);
if(aux==n) return 1;
else return 0;
}

int main() {
int i,n;
fscanf(stdin,"%d",&n);
for(i=0;i<n;++i) {
long valor,inv,soma;
int nit=1;
fscanf(stdin,"%d",&valor);
inv=reverse(valor);
soma = valor + inv;
while(palindrome(soma)==0) {
++nit;
valor=soma;
inv=reverse(valor);
soma = valor + inv;
}
fprintf(stdout,"%d %d\n",nit,soma);
}

return 0;
}

[/c]

thanks !!

passwd
New poster
Posts: 14
Joined: Thu Dec 05, 2002 11:20 pm

Post by passwd » Tue May 27, 2003 2:51 am

hi, I'm trying to solve this problem too , but I'm getting WA and I don't
know why ??

someone can help me ??

[c]
#include <stdio.h>
/*#define DEBUG*/

long reverse(long n) {
long fator=1;
long aux=n,resp=1;
while(aux!=0) { fator*=10; aux = aux / 10; }
fator/=10;
/* printf("aux: %d fator:%d \n",aux,fator); */
do {
aux=n % 10;
resp = resp + aux * fator;
n= n / 10;
fator/=10;
} while(n!=0);
--resp;
return resp;
}

int palindrome(long n) {
long aux=reverse(n);
if(aux==n) return 1;
else return 0;
}

int main() {
int i,n;
fscanf(stdin,"%d",&n);
for(i=0;i<n;++i) {
long valor,inv,soma;
int nit=1;
fscanf(stdin,"%d",&valor);
inv=reverse(valor);
soma = valor + inv;
while(palindrome(soma)==0) {
++nit;
valor=soma;
inv=reverse(valor);
soma = valor + inv;
}
fprintf(stdout,"%d %d\n",nit,soma);
}

return 0;
}

[/c]

thanks !!

kpras
New poster
Posts: 2
Joined: Thu Jun 12, 2003 2:15 pm
Location: IIT KANPUR, INDIA
Contact:

10018

Post by kpras » Wed Aug 20, 2003 12:09 pm

[java] /**
* Description :
* Date :16.08.2003
* Status :
* @author Prashant Kumar(prashkr@iitk.ac.in ,kpras@email.com)
*/
import java.io.IOException;
import java.util.StringTokenizer;
class Main{
private String number;

Main(int number){
this.number = ""+number;
}
public static void main(String[] args)
throws IOException {
StringTokenizer token = new StringTokenizer(readInput() );
int count = Integer.parseInt(token.nextToken());
Main obj;
for(int i = 0; i < count; i++) {
obj = new Main(Integer.parseInt(token.nextToken()));
System.out.println(obj.getResult());
}

}//end of main

public static void test(){
Main obj = new Main(195);
System.out.println(obj.getResult());
obj = new Main(265);
System.out.println(obj.getResult());
obj = new Main(750);
System.out.println(obj.getResult());

}

public String getResult() {
boolean palendrome = false;
int count = 0;
while(!palendrome){
palendrome = isPalendrome(number = reverseAndAdd(number));
count++;
}

return count+ " "+number;
}

public boolean isPalendrome(String string){
boolean palendrome = true;
char[] array = string.toCharArray();
int length = array.length;
int half = length / 2;

for(int i = 0; (i < half) && (palendrome); i++) {
if(array != array[length -1 -i]) {
palendrome = false;
}
}

return palendrome;
}

public String reverseAndAdd(String number) {
int length;
char[] array = number.toCharArray();
char[] reverse = new char[length = array.length];
for(int i = 0; i < length; i++) {
reverse = array[length -1 - i];
}
return ""+(Integer.parseInt(number)+ Integer.parseInt(new String(reverse)));//""+ to convert the int to string

}


private static String readInput()
throws IOException {
StringBuffer buffer = new StringBuffer();
boolean done = false;
int read = System.in.read();
while( !done) {
if( read != -1)
{
buffer.append( (char)read );
read = System.in.read();
}
else
{
done = true;
}
}
return buffer.toString();

}//end of readInput()

}//end of class[/java]

User avatar
chanwen
New poster
Posts: 2
Joined: Wed Nov 05, 2003 11:48 am
Location: Taipei, Taiwan
Contact:

What is the right output for the input 99 now?

Post by chanwen » Wed Nov 05, 2003 12:55 pm

I got confused now.
According to the problem description, "If the sum is not a palindrome, ..."
Doesn't the term "sum" mean I have to do "reverse and add" at lease one time?

However, my on-line judge's result varies from WA, Time Limit Exceeeded, Run Time Error, ....

And after I change my code for the already-palindrome input, I got Accepted.

So what on earth is the correct answer?
Hanwen Chang
Taiwan

Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan » Sat Nov 08, 2003 4:03 pm

For each of the N tests you will have to write a line with the following data : minimum number of iterations (additions) to get to the palindrome and the resulting palindrome itself separated by one space.
See the word 'minimum'. That signals that the number of iteration might be zero (no iteration at all) because the objective is to make a palindrome out of the given input. If the input is already a palindrome, the objective is accomplished!! :wink: :wink:
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.

User avatar
chanwen
New poster
Posts: 2
Joined: Wed Nov 05, 2003 11:48 am
Location: Taipei, Taiwan
Contact:

Alright, a communicating misunderstanding.

Post by chanwen » Sat Nov 08, 2003 5:23 pm

Because when I see the word "sum," I directly guess I should deal with all input with "reverse and add" at least one time. I think the description should be clearfied about the proper output format when N is already a palindrome.

I am not a native English user, so sometimes the interpretion/translation makes little differences from original in some viewpoints. Is it possible to make the description more detailed so no one will ask again about this problem.

By the way, this problem became one in my C programming midterm this week. What a coincidence!
Hanwen Chang
Taiwan

problem
New poster
Posts: 27
Joined: Mon Nov 10, 2003 12:40 am

10018 RE why help me

Post by problem » Mon Nov 10, 2003 2:24 am

i dont understand why its runtime error.plz find out my prob and give me solution.



/* @JUDGE_ID: xxxxxx 10018 C++ */

#include<stdio.h>
#include<math.h>
int q=0;
long double fact(long double i);
main()
{
int z,q;
long double i,j,t;
scanf("%d",&z);
for(q=0;q<z;q++)
{
scanf("%Lf",&i);
t=fact(i);
printf("%.0Lf\n",t);
}
}

long double fact(long double i)
{
q++;
long double j,x[250],sum=0,a;
int p,t;
a=i;
for(t=0;i>=1;t++)
{
j=fmodl(i,10);
x[t]=j;
i=floor(i/10);
}
for(t=t-1,p=0;t>=0;t--,p++)
{
sum=sum+x[p]*(powl(10,t));
}
if(sum==a)
{
printf("%d ",q-1);
q=0;
return a;
}
return fact(a+sum);
}

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel » Mon Nov 10, 2003 9:05 am

Hi there,

Your program actually gets compile error.
You can not use fmodl, and other functions like that.

I have changed your code and got it AC.

1. Change long double to double.
2. Change fmodl to fmod.
3. Change Lf to lf.

Hope it helps.

osan
New poster
Posts: 47
Joined: Tue Jul 29, 2003 12:03 pm
Location: Bangladesh,Dhaka.
Contact:

Post by osan » Mon Nov 10, 2003 5:03 pm

i think if you dont wanna use

1. Change long double to double.
2. Change fmodl to fmod.
3. Change Lf to lf.

u can use unsigned long.
but u have to use %lu.
this time WA
what next...............?

bayzid
New poster
Posts: 7
Joined: Sun Sep 14, 2003 8:09 am
Location: bangladesh
Contact:

10018 at last ac

Post by bayzid » Tue Nov 11, 2003 4:57 pm

thank everybody to give infomation.at last i ac the problem
arahman

WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

10018 Reverse and Add

Post by WR » Wed Dec 10, 2003 9:53 am

I thought problems 333 (ISBN) and 10018 (Reverse and Add) were
easy - ha!

Has anybody a suggestions for 10018?!

My input
195 -> 4 9339
265 -> 5 45254
750 -> 3 6666
2 -> 1 4
99 -> 6 79497
6 -> 2 33
4000000000 -> 1 4000000004
20 -> 1 22
100 - > 1 101

I got a WA for that. Then I ignored input that already is a
palindrome -> WA. Then I output zero iterations for that input, e.g.
99 -> 0 99, led to WA as well.

Where's my mistake?
Last edited by WR on Wed Dec 10, 2003 1:22 pm, edited 1 time in total.

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel » Wed Dec 10, 2003 10:11 am

Hi,
your input and output seems to be correct.
And if the original number is a palindrome then output 0 N
May be you made a mistake somewhere else.
Perhaps posting your code will be a good idea.
:oops:

Post Reply

Return to “Volume 100 (10000-10099)”