PalindromeProblem code: DPC206 |
All submissions for this problem are available.
Palindrome
A simple recursive method to generate a numeric palindrome from any number is to reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number from left to right and right to left), repeat this procedure. For example for 195:
195 + 951 = 786
786 + 687 = 1473
1473 + 3741 = 5214
5214 + 4125 = 9339 Resulting palindrome
In this particular case the palindrome 9339 appeared after 4th addition. This method leads to palindromes in a few step for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It is not proven though, that there is no such a palindrome.
Your task is to write a program that gives the resulting palindrome and the number of iterations (additions) to compute it. All tests data in this problem will have an answer, will be computable with less than 1000 iterations (additions), which will yield a palindrome that is not greater than 4,294,967,295.
Input
The first line will have a number N (0<N<=100) with the number of test cases, the next N lines will each have a number P to compute its palindrome.
Output
For each of the N numbers you will have to write a line with the minimum number of iterations (additions) to get to the palindrome and the resulting palindrome separated by one space.
Sample Input
3
195
265
750
Sample Output
4 9339
5 45254
3 6666
| Author: | rushikesh30 |
| Date Added: | 10-03-2010 |
| Time Limit: | 5 sec |
| Source Limit: | 50000 Bytes |
| Languages: | ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC |
Comments
SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:
HELP
Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:
- Accepted
Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark. - Time Limit Exceeded
Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach. - Wrong Answer
Your program compiled and ran succesfully but the output did not match the expected output. - Runtime Error
Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section. - Compilation Error
Your code was unable to compile. When you see this icon, click on it for more information.
If you are still having problems, see a sample solution here.

Fetching successful submissions

There is a typo in your
There is a typo in your example. It should be "195 + 591 = 786", not "195 + 951 = 786".
@admin I am getting a runtime
@admin
I am getting a runtime error for my code in Python. Its working fine on my system. Can you push me a little bit so that I can solve this ques? My submission id is 270474
/* this code working fine
Input : 196 Output 75
Input :
196
Output
75 919969919
My code
My code successfully compiles and runs on jdk 1.3 on my PC. but on the website it shows compilation error saying" compilation is successfull but main class is not found".the folowing is my code.Can someone please help.Are there any conventions for naming the main class.?? (thank you)
import java.io.*;
class palind
{
long reverse(long b)
{
long c=0;
while(b!=0)
{
c=c*10+b%10;
b/=10;
}
return(c);
}
}
class chef_palind
{
public static void main(String[] args)throws IOException
{
int i,j,n;
palind p=new palind();
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(b.readLine());
long a[]=new long[n+1];
for(i=1;i<=n;i++)
a[i]=Long.parseLong(b.readLine());
for(i=1;i<=n;i++)
{
j=0;
while(p.reverse(a[i])!=a[i])
{
j++;
a[i]+=p.reverse(a[i]);
}
System.out.println(j+" "+a[i]+"n");
}
}
}
Read the FAQ.
Read the FAQ.
Thank you Ayush..that was so
Thank you Ayush..that was so stupid of me..!!
i kno its dumb but cn anyone
i kno its dumb but cn anyone pls tell me how exactly to take the input?
my programme is running successfully in borland c++;
but here even though i hv used the required syntax for c++(gcc++ 4.3.2) it says wrong answer :(
// Palindrome.cpp : Defines
// Palindrome.cpp : Defines the entry point for the console application.
//
#include<iostream>
#include<stdio.h>
using namespace std;
struct pali
{
int a,count;
};
struct pali p[100];
class pm
{
public:
int rev(int i);
int rev_a(int j);
int add(int a, int b);
private:
int n1,n2;
};
int pm::add(int a, int b)
{
int sum;
sum = a + b;
return sum;
}
int pm::rev(int i)
{
int temp=0,test = i,s_result = test;
p->count = 0;
thisplace:
n2 = s_result;
test = n2;
temp = 0;
while(test!=0)
{
temp*=10;
temp +=test%10;
test /=10;
}
n1=temp;
p->count += 1;
if(n1 != n2)
{
s_result = pm::add(n1,n2);
goto thisplace;
}
if(n1 == n2)
{
p->count--;
cout << p->count << " " ;
return n1;
}
else
cout <<"No match ";
return 0;
}
int main()
{
pm myPM;
int n;
cout << "How many line do you want : ";
cin >> n;
cout << "Enter numbers : ";
for(int i = 0; i<n; i++)
{
cin >> p[i].a;
}
int equal,equal2;
for(int i=0; i<n; i++)
{
equal2 = p[i].a;
cout <<myPM.rev(equal2) <<endl;
}
return 0;
}
@Devarsh : Input
@Devarsh :
Input :
196
Output
44 588755786687557885
oh god...cant believe
oh god...cant believe this...even missing out a "n" can get one a wrong answer here..
by "\n" i mean next line
by "n" i mean next line character
Of course it can; if you
Of course it can; if you don't print any space between your answers, how could the judge know where one answer ends and the next one starts?
Should'nt this problem be in
Should'nt this problem be in the easy section???
please check my program whats
please check my program whats wrong in it
my python code runs perfectly
umm.... this line: 195 + 951
Hello Guys.The example is
pretty easy problem this
can someone please tell me
i'm geeting answers correct
wat therun time mean???
Ans of 2nd test case is 8888
int power(int a,int
int power(int a,int
#include #include int
@admin:My program is showing