A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. 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.
1 #include<iostream> 2 #include<string> 3 #define MAX 7 4 using namespace std; 5 6 int arr[MAX]; 7 int ptr; 8 string ss=""; 9 void pushstack(int t) 10 { 11 arr[ptr]=t; 12 ptr++; 13 } 14 15 int popstack() 16 { 17 ptr--; 18 return arr[ptr]; 19 } 20 21 void Palindrome(int m) 22 { 23 m=m+1; 24 int i,j; 25 26 while(1) 27 { 28 i=m; 29 j=m; 30 ptr=0; 31 bool tag=false;//标记 32 while(i>0) 33 { 34 int k; 35 k=i%10; 36 pushstack(k); 37 i=i/10; 38 } 39 while(j>0) 40 { 41 int k; 42 k=j%10; 43 int f=popstack(); 44 if(k!=f) 45 break; 46 else if(k==f&&ptr==0) 47 { 48 tag=true; 49 char s[MAX]; 50 sprintf(s,"%d\n",m); 51 ss=ss+s; 52 } 53 j=j/10; 54 } 55 if(tag==true) 56 break; 57 m=m+1; 58 } 59 60 } 61 62 int main() 63 { 64 int i; 65 cin>>i; 66 while(i>0) 67 { 68 int j; 69 cin>>j; 70 i--; 71 Palindrome(j); 72 } 73 cout<<ss<<endl; 74 return 0; 75 }
代码截图:
2016-12-3114:24:08 By--LeeVanVan
PALIN - The Next Palindrome 对称的数
原文:http://www.cnblogs.com/geekvan/p/6239220.html