1 / 4
Apr 2016

include

using namespace std;
int palindrome(float x){
int reverse=0,temp;
temp=x;
while(temp!=0){
reverse=reverse*10;
reverse=reverse+temp%10;
temp=temp/10;
}
if(x==reverse)
return 1;
else
return 0;
}
int main(){
int t,ans;
float K;
cin>>t;
while(t--){
cin>>K;
do{
ans=palindrome(K);
if(ans==1)
break;
K++;
}while(ans!=1);
cout< }
return 0;
}
(Last cout is K which is the next palindrome.)
Im given Time limit exceeded error for the above code, please help me to point out how to reduce the time required for running. Suggestions are welcomed.
By the way the code is alright. U might have to understand few lines because this post is not exactly posting the same code as of mine.
Thanks.

  • created

    Apr '16
  • last reply

    Apr '16
  • 3

    replies

  • 1.1k

    views

  • 2

    users

  • 2

    links

  • It is possible (and not that difficult) to post code properly. If you would read other threads within this forum you would see that it is possible, and you can find information how
  • The problem statement says that you have to process a "positive integer K of not more than 1000000 digits". I doubt that float (nor any other numeric data type) is able to handle this requirement.

But float datatype cannot be used as %(modulus) function is to be used on the value of the input.

True. But you still have to find a data type which is able to store 1 000 000 digits ( a number as big as 10^1000000) without precision loss.

I’m wondering why no-one searches this forum before posting…