samedi 18 avril 2015

Finding the next palindrome

For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.


The first line contains integer t, the number of test cases. Integers K are given in the next t lines.


For each K, output the smallest palindrome larger than K.


Here is my code.



#include <stdio.h>

int pal(int n)
{
int temp,rev=0;
temp=n;
while(temp)
{
rev=rev*10+(temp%10);
temp/=10;
}
if(n==rev)
{
return 0;
}
return 1;
}
int main()
{

int n;
scanf("%d",&n);
int i=0,j,num[n];

while(n--)
{
scanf("%d",&num[i++]);
}
for(i=0;i<n;i++)
{
j=num[i]+1;
while(j>num[i])
{
if(pal(j)==0)
{
printf("%d",j);
break;
}
else
{
j++;
}
}
printf("\n");
}
return 0;
}


I cannot guess what is the error. Someone help. I'm not getting the output


Aucun commentaire:

Enregistrer un commentaire