10018 - Reverse and Add
Moderator: Board moderators
-
- Learning poster
- Posts: 93
- Joined: Sun Jan 12, 2003 3:30 pm
-
- Experienced poster
- Posts: 187
- Joined: Wed Dec 11, 2002 2:03 pm
- Location: Mount Papandayan, Garut
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 !!
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 !!
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 !!
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 !!
10018
[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]
* 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]
What is the right output for the input 99 now?
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?
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
Taiwan
-
- Experienced poster
- Posts: 136
- Joined: Tue Apr 01, 2003 6:59 am
- Location: Jakarta, Indonesia
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!!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.


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.
Alright, a communicating misunderstanding.
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!
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
Taiwan
10018 RE why help me
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);
}
/* @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);
}
10018 Reverse and Add
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?
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.